Skip to content

Commit

Permalink
Improve performance and reduce memory allocation (#4521)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Oct 3, 2024
1 parent 6a66daa commit 58ea2b5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
11 changes: 7 additions & 4 deletions compat/src/forwardRef.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { options } from 'preact';
import { assign } from './util';

let oldDiffHook = options._diff;
options._diff = vnode => {
Expand All @@ -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
Expand Down
12 changes: 3 additions & 9 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }} */
Expand Down
3 changes: 2 additions & 1 deletion jsx-runtime/test/browser/jsx-runtime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 58ea2b5

Please sign in to comment.