From 5de3be25a9795aa7eff770d276e298f42d711875 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 2 Oct 2024 18:27:02 +0200 Subject: [PATCH] Improve performance and reduce memory allocation --- compat/src/forwardRef.js | 13 +++++++++---- jsx-runtime/src/index.js | 12 +++--------- jsx-runtime/test/browser/jsx-runtime.test.js | 3 ++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/compat/src/forwardRef.js b/compat/src/forwardRef.js index 25791285b9..32aef0728a 100644 --- a/compat/src/forwardRef.js +++ b/compat/src/forwardRef.js @@ -1,5 +1,4 @@ import { options } from 'preact'; -import { assign } from './util'; let oldDiffHook = options._diff; options._diff = vnode => { @@ -25,9 +24,15 @@ export const REACT_FORWARD_SYMBOL = */ export function forwardRef(fn) { function Forwarded(props) { - let clone = assign({}, props); - delete clone.ref; - return fn(clone, props.ref || null); + if (!('ref' in props)) return fn(props, null); + + let ref = props.ref; + try { + delete props.ref; + return fn(props, ref || null); + } finally { + props.ref = ref; + } } // mobx-react checks for this being present diff --git a/jsx-runtime/src/index.js b/jsx-runtime/src/index.js index 454df6308a..20be27de90 100644 --- a/jsx-runtime/src/index.js +++ b/jsx-runtime/src/index.js @@ -35,15 +35,9 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) { ref, i; - if ('ref' in normalizedProps) { - normalizedProps = {}; - for (i in props) { - if (i == 'ref') { - ref = props[i]; - } else { - normalizedProps[i] = props[i]; - } - } + if ('ref' in props) { + ref = props.ref; + delete props.ref; } /** @type {VNode & { __source: any; __self: any }} */ diff --git a/jsx-runtime/test/browser/jsx-runtime.test.js b/jsx-runtime/test/browser/jsx-runtime.test.js index ac678ce477..c3b338d2db 100644 --- a/jsx-runtime/test/browser/jsx-runtime.test.js +++ b/jsx-runtime/test/browser/jsx-runtime.test.js @@ -37,7 +37,8 @@ describe('Babel jsx/jsxDEV', () => { const props = { ref }; const vnode = jsx('div', props); expect(vnode.ref).to.equal(ref); - expect(vnode.props).to.not.equal(props); + expect(vnode.props).to.equal(props); + expect(vnode.props.ref).to.equal(undefined); }); it('should not copy props wen there is no ref in props', () => {