Skip to content

Commit 79303ec

Browse files
committed
Improve performance and reduce memory allocation
1 parent 6a66daa commit 79303ec

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

compat/src/forwardRef.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { options } from 'preact';
2-
import { assign } from './util';
32

43
let oldDiffHook = options._diff;
54
options._diff = vnode => {
@@ -25,9 +24,13 @@ export const REACT_FORWARD_SYMBOL =
2524
*/
2625
export function forwardRef(fn) {
2726
function Forwarded(props) {
28-
let clone = assign({}, props);
29-
delete clone.ref;
30-
return fn(clone, props.ref || null);
27+
if (!('ref' in props)) return fn(props, null);
28+
29+
let ref = props.ref;
30+
delete props.ref;
31+
const result = fn(props, ref);
32+
props.ref = ref;
33+
return result;
3134
}
3235

3336
// mobx-react checks for this being present

jsx-runtime/src/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) {
3535
ref,
3636
i;
3737

38-
if ('ref' in normalizedProps) {
39-
normalizedProps = {};
40-
for (i in props) {
41-
if (i == 'ref') {
42-
ref = props[i];
43-
} else {
44-
normalizedProps[i] = props[i];
45-
}
46-
}
38+
if ('ref' in props) {
39+
ref = props.ref;
40+
delete props.ref;
4741
}
4842

4943
/** @type {VNode & { __source: any; __self: any }} */

jsx-runtime/test/browser/jsx-runtime.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ describe('Babel jsx/jsxDEV', () => {
3737
const props = { ref };
3838
const vnode = jsx('div', props);
3939
expect(vnode.ref).to.equal(ref);
40-
expect(vnode.props).to.not.equal(props);
40+
expect(vnode.props).to.equal(props);
41+
expect(vnode.props.ref).to.equal(undefined);
4142
});
4243

4344
it('should not copy props wen there is no ref in props', () => {

0 commit comments

Comments
 (0)