From 58ea2b544aab03ea22b273af2ef0c73b254396eb Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Thu, 3 Oct 2024 10:36:51 +0200 Subject: [PATCH] Improve performance and reduce memory allocation (#4521) --- compat/src/forwardRef.js | 11 +++++++---- jsx-runtime/src/index.js | 12 +++--------- jsx-runtime/test/browser/jsx-runtime.test.js | 3 ++- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/compat/src/forwardRef.js b/compat/src/forwardRef.js index 25791285b9..35105eadc5 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,13 @@ 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; + delete props.ref; + const result = fn(props, ref); + props.ref = ref; + return result; } // 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', () => {