preact icon indicating copy to clipboard operation
preact copied to clipboard

Ignore undefined children in createElement 3rd arg

Open developit opened this issue 3 years ago • 6 comments

I believe #3091 may have introduced a bug where h('div', { children: 'foo' }, undefined) would produce { children: undefined }. This fixes that bug, if it is a bug.

I've left this as a draft because I actually don't know which is the more desirable behavior here. From an implementation standpoint, I actually think the arguments.length > 2 approach is best, because it allows us to seamlessly transition to rest parameters since the semantics are then the same:

function createElement(type, props, ...children) {
  if (children.length !== 0) props.children = children;
}

developit avatar Apr 15 '21 20:04 developit

📊 Tachometer Benchmark Results

Summary

duration

  • 02_replace1k: unsure 🔍 -1% - +2% (-1.33ms - +2.53ms)
    preact-local vs preact-master
  • 03_update10th1k_x16: unsure 🔍 -5% - +5% (-1.69ms - +1.58ms)
    preact-local vs preact-master
  • 07_create10k: unsure 🔍 -1% - +0% (-9.18ms - +7.82ms)
    preact-local vs preact-master
  • filter_list: unsure 🔍 -5% - +1% (-1.56ms - +0.49ms)
    preact-local vs preact-master
  • hydrate1k: unsure 🔍 -2% - +1% (-2.62ms - +0.90ms)
    preact-local vs preact-master
  • many_updates: unsure 🔍 -5% - +7% (-1.72ms - +2.64ms)
    preact-local vs preact-master
  • text_update: unsure 🔍 -8% - +3% (-0.29ms - +0.10ms)
    preact-local vs preact-master

usedJSHeapSize

  • 02_replace1k: unsure 🔍 -0% - +0% (-0.01ms - +0.00ms)
    preact-local vs preact-master
  • 03_update10th1k_x16: unsure 🔍 -0% - +0% (-0.02ms - +0.01ms)
    preact-local vs preact-master
  • 07_create10k: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-master
  • filter_list: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-master
  • hydrate1k: unsure 🔍 -0% - +1% (-0.01ms - +0.05ms)
    preact-local vs preact-master
  • many_updates: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-master
  • text_update: unsure 🔍 -0% - -0% (-0.00ms - -0.00ms)
    preact-local vs preact-master

Results

02_replace1k
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 80
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master157.57ms - 159.81ms-unsure 🔍
-2% - +1%
-2.53ms - +1.33ms
preact-local157.72ms - 160.86msunsure 🔍
-1% - +2%
-1.33ms - +2.53ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master3.58ms - 3.59ms-unsure 🔍
-0% - +0%
-0.00ms - +0.01ms
preact-local3.57ms - 3.58msunsure 🔍
-0% - +0%
-0.01ms - +0.00ms
-

run-warmup-0

VersionAvg timevs preact-mastervs preact-local
preact-master57.80ms - 58.46ms-unsure 🔍
-1% - +1%
-0.44ms - +0.59ms
preact-local57.66ms - 58.45msunsure 🔍
-1% - +1%
-0.59ms - +0.44ms
-

run-warmup-1

VersionAvg timevs preact-mastervs preact-local
preact-master90.79ms - 91.56ms-unsure 🔍
-1% - +1%
-0.82ms - +0.48ms
preact-local90.82ms - 91.87msunsure 🔍
-1% - +1%
-0.48ms - +0.82ms
-

run-warmup-2

VersionAvg timevs preact-mastervs preact-local
preact-master98.03ms - 103.83ms-unsure 🔍
-3% - +6%
-2.69ms - +6.14ms
preact-local95.87ms - 102.54msunsure 🔍
-6% - +3%
-6.14ms - +2.69ms
-

run-warmup-3

VersionAvg timevs preact-mastervs preact-local
preact-master59.43ms - 63.57ms-unsure 🔍
-7% - +3%
-4.35ms - +2.01ms
preact-local60.26ms - 65.09msunsure 🔍
-3% - +7%
-2.01ms - +4.35ms
-

run-warmup-4

VersionAvg timevs preact-mastervs preact-local
preact-master94.16ms - 99.55ms-unsure 🔍
-4% - +3%
-4.35ms - +3.11ms
preact-local94.90ms - 100.06msunsure 🔍
-3% - +5%
-3.11ms - +4.35ms
-

run-final

VersionAvg timevs preact-mastervs preact-local
preact-master58.20ms - 59.90ms-unsure 🔍
-3% - +2%
-1.71ms - +0.91ms
preact-local58.46ms - 60.44msunsure 🔍
-2% - +3%
-0.91ms - +1.71ms
-
03_update10th1k_x16
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 60
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master33.06ms - 35.62ms-unsure 🔍
-5% - +5%
-1.58ms - +1.69ms
preact-local33.28ms - 35.30msunsure 🔍
-5% - +5%
-1.69ms - +1.58ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master3.63ms - 3.65ms-unsure 🔍
-0% - +0%
-0.01ms - +0.02ms
preact-local3.63ms - 3.65msunsure 🔍
-0% - +0%
-0.02ms - +0.01ms
-
07_create10k
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 50
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master1647.48ms - 1659.56ms-unsure 🔍
-0% - +1%
-7.82ms - +9.18ms
preact-local1646.87ms - 1658.82msunsure 🔍
-1% - +0%
-9.18ms - +7.82ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master25.97ms - 25.97ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-local25.97ms - 25.97msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
filter_list
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 70
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master32.30ms - 34.18ms-unsure 🔍
-1% - +5%
-0.49ms - +1.56ms
preact-local32.31ms - 33.11msunsure 🔍
-5% - +1%
-1.56ms - +0.49ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master1.58ms - 1.58ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-local1.58ms - 1.58msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
hydrate1k
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 50
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master141.81ms - 143.74ms-unsure 🔍
-1% - +2%
-0.90ms - +2.62ms
preact-local140.44ms - 143.38msunsure 🔍
-2% - +1%
-2.62ms - +0.90ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master6.17ms - 6.20ms-unsure 🔍
-1% - +0%
-0.05ms - +0.01ms
preact-local6.18ms - 6.23msunsure 🔍
-0% - +1%
-0.01ms - +0.05ms
-
many_updates
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 80
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master34.33ms - 37.22ms-unsure 🔍
-7% - +5%
-2.64ms - +1.72ms
preact-local34.60ms - 37.87msunsure 🔍
-5% - +7%
-1.72ms - +2.64ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master4.83ms - 4.83ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-local4.83ms - 4.83msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
text_update
  • Browser: chrome-headless 89.0.4389.114
  • Sample size: 250
  • Built by: Benchmarks #215
  • Commit: 5b2dc48

duration

VersionAvg timevs preact-mastervs preact-local
preact-master3.37ms - 3.73ms-unsure 🔍
-3% - +8%
-0.10ms - +0.29ms
preact-local3.39ms - 3.52msunsure 🔍
-8% - +3%
-0.29ms - +0.10ms
-

usedJSHeapSize

VersionAvg timevs preact-mastervs preact-local
preact-master0.98ms - 0.98ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-local0.98ms - 0.98msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-

tachometer-reporter-action v2 for Benchmarks

github-actions[bot] avatar Apr 15 '21 20:04 github-actions[bot]

Size Change: +2 B (0%)

Total Size: 42.4 kB

Filename Size Change
dist/preact.module.js 4.01 kB +1 B
dist/preact.umd.js 4.05 kB +1 B
ℹ️ View Unchanged
Filename Size Change
compat/dist/compat.js 3.51 kB 0 B
compat/dist/compat.module.js 3.52 kB 0 B
compat/dist/compat.umd.js 3.56 kB 0 B
debug/dist/debug.js 2.99 kB 0 B
debug/dist/debug.module.js 2.98 kB 0 B
debug/dist/debug.umd.js 3.07 kB 0 B
devtools/dist/devtools.js 232 B 0 B
devtools/dist/devtools.module.js 241 B 0 B
devtools/dist/devtools.umd.js 308 B 0 B
dist/preact.js 3.98 kB 0 B
dist/preact.min.js 4.01 kB 0 B
hooks/dist/hooks.js 1.13 kB 0 B
hooks/dist/hooks.module.js 1.15 kB 0 B
hooks/dist/hooks.umd.js 1.2 kB 0 B
jsx-runtime/dist/jsxRuntime.js 327 B 0 B
jsx-runtime/dist/jsxRuntime.module.js 335 B 0 B
jsx-runtime/dist/jsxRuntime.umd.js 405 B 0 B
test-utils/dist/testUtils.js 437 B 0 B
test-utils/dist/testUtils.module.js 439 B 0 B
test-utils/dist/testUtils.umd.js 515 B 0 B

compressed-size-action

github-actions[bot] avatar Apr 15 '21 20:04 github-actions[bot]

Coverage Status

Coverage remained the same at 99.446% when pulling 5b2dc4813bc13b6338ef801fa2d23de11779059f on createelement-undefined-children into 4cdcc88afde9000c1b2e1b5d6e6f0a96f8f39ca6 on master.

coveralls avatar Apr 15 '21 20:04 coveralls

IMO, we should match the behavior of react and babel-plugin-transform-react-jsx with the new jsx runtime.

Babel with the new jsx runtime (which preact also supports with preact/jsx-runtime) transpiles

<div children="foo">{undefined}</div>

to

import { jsx as _jsx } from "preact/jsx-runtime";

/*#__PURE__*/
_jsx("div", {
  children: "foo",
  children: undefined
});

clyfish avatar Apr 16 '21 02:04 clyfish

That is a bug in Babel and doesn't affect createElement(), it only affects jsxs()

developit avatar Apr 17 '21 16:04 developit

The bug affects both React.createElement and Babel, if it is a bug. I think Preact needs to be consistent with React and Babel, whether it is a bug or not. https://github.com/facebook/react/blob/v17.0.2/packages/react/src/ReactElement.js#L386-L400

clyfish avatar Apr 18 '21 15:04 clyfish