Skip to content

Commit

Permalink
Improve performance and reduce memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 2, 2024
1 parent 703af77 commit 33d1496
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
13 changes: 9 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,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
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 33d1496

Please sign in to comment.