From 8524bb87dfffd8450ca5747052a1615805be44ee Mon Sep 17 00:00:00 2001 From: jdecroock Date: Thu, 11 Apr 2024 10:55:14 +0200 Subject: [PATCH 1/5] optimise jsx-runtime by only creating a new object when ref is passed --- jsx-runtime/src/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/jsx-runtime/src/index.js b/jsx-runtime/src/index.js index 8cbe9ea444..f6e1b13101 100644 --- a/jsx-runtime/src/index.js +++ b/jsx-runtime/src/index.js @@ -30,14 +30,15 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) { // We'll want to preserve `ref` in props to get rid of the need for // forwardRef components in the future, but that should happen via // a separate PR. - let normalizedProps = {}, + let normalizedProps = props || {}, ref, i; - for (i in props) { - if (i == 'ref') { - ref = props[i]; - } else { - normalizedProps[i] = props[i]; + if (normalizedProps && (ref = normalizedProps.ref)) { + normalizedProps = {}; + for (i in props) { + if (i != 'ref') { + normalizedProps[i] = props[i]; + } } } From 8a060b950c4a92fed8103ecf2f555657ee3cf1a0 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Thu, 11 Apr 2024 11:02:13 +0200 Subject: [PATCH 2/5] add test --- jsx-runtime/test/browser/jsx-runtime.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/jsx-runtime/test/browser/jsx-runtime.test.js b/jsx-runtime/test/browser/jsx-runtime.test.js index b43b08ec71..ac678ce477 100644 --- a/jsx-runtime/test/browser/jsx-runtime.test.js +++ b/jsx-runtime/test/browser/jsx-runtime.test.js @@ -34,8 +34,16 @@ describe('Babel jsx/jsxDEV', () => { it('should keep ref in props', () => { const ref = () => null; - const vnode = jsx('div', { ref }); + const props = { ref }; + const vnode = jsx('div', props); expect(vnode.ref).to.equal(ref); + expect(vnode.props).to.not.equal(props); + }); + + it('should not copy props wen there is no ref in props', () => { + const props = { x: 'y' }; + const vnode = jsx('div', props); + expect(vnode.props).to.equal(props); }); it('should add keys', () => { From 4f085f19953dd8259dfc907cca10a2dfb265d722 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Thu, 11 Apr 2024 11:06:33 +0200 Subject: [PATCH 3/5] shave --- jsx-runtime/src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jsx-runtime/src/index.js b/jsx-runtime/src/index.js index f6e1b13101..f4994ef5d3 100644 --- a/jsx-runtime/src/index.js +++ b/jsx-runtime/src/index.js @@ -27,13 +27,14 @@ const isArray = Array.isArray; * @param {unknown} [__self] */ function createVNode(type, props, key, isStaticChildren, __source, __self) { + if (!props) props = {}; // We'll want to preserve `ref` in props to get rid of the need for // forwardRef components in the future, but that should happen via // a separate PR. - let normalizedProps = props || {}, + let normalizedProps = props, ref, i; - if (normalizedProps && (ref = normalizedProps.ref)) { + if ((ref = normalizedProps.ref)) { normalizedProps = {}; for (i in props) { if (i != 'ref') { From d9852257c5d22b605aacca9e12fddb00021699c0 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Wed, 17 Apr 2024 16:27:23 +0200 Subject: [PATCH 4/5] Update jsx-runtime/src/index.js Co-authored-by: Jason Miller --- jsx-runtime/src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jsx-runtime/src/index.js b/jsx-runtime/src/index.js index f4994ef5d3..ce9fe3803f 100644 --- a/jsx-runtime/src/index.js +++ b/jsx-runtime/src/index.js @@ -34,10 +34,12 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) { let normalizedProps = props, ref, i; - if ((ref = normalizedProps.ref)) { + if ('ref' in normalizedProps) { normalizedProps = {}; for (i in props) { if (i != 'ref') { + ref = props[i]; + } else { normalizedProps[i] = props[i]; } } From 7cf88e52eb3c2f41bd3b6739484b57234de654af Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 17 Apr 2024 16:32:00 +0200 Subject: [PATCH 5/5] fix --- jsx-runtime/src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jsx-runtime/src/index.js b/jsx-runtime/src/index.js index ce9fe3803f..144ac83712 100644 --- a/jsx-runtime/src/index.js +++ b/jsx-runtime/src/index.js @@ -34,10 +34,11 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) { let normalizedProps = props, ref, i; + if ('ref' in normalizedProps) { normalizedProps = {}; for (i in props) { - if (i != 'ref') { + if (i == 'ref') { ref = props[i]; } else { normalizedProps[i] = props[i];