Skip to content

Commit

Permalink
feat: optimise jsx runtime (#4337)
Browse files Browse the repository at this point in the history
* optimise jsx-runtime by only creating a new object when ref is passed

* add test

* shave

* Update jsx-runtime/src/index.js

Co-authored-by: Jason Miller <[email protected]>

* fix

---------

Co-authored-by: Jason Miller <[email protected]>
  • Loading branch information
JoviDeCroock and developit authored Apr 17, 2024
1 parent 8c88f52 commit f3edc90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 11 additions & 6 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ const isArray = Array.isArray;
* @param {unknown} [__self]
*/
function createVNode(type, props, key, isStaticChildren, __source, __self) {
if (!props) props = {};
// We'll want to preserve `ref` in props to get rid of the need for
// forwardRef components in the future, but that should happen via
// a separate PR.
let normalizedProps = {},
let normalizedProps = props,
ref,
i;
for (i in props) {
if (i == 'ref') {
ref = props[i];
} else {
normalizedProps[i] = props[i];

if ('ref' in normalizedProps) {
normalizedProps = {};
for (i in props) {
if (i == 'ref') {
ref = props[i];
} else {
normalizedProps[i] = props[i];
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion jsx-runtime/test/browser/jsx-runtime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ describe('Babel jsx/jsxDEV', () => {

it('should keep ref in props', () => {
const ref = () => null;
const vnode = jsx('div', { ref });
const props = { ref };
const vnode = jsx('div', props);
expect(vnode.ref).to.equal(ref);
expect(vnode.props).to.not.equal(props);
});

it('should not copy props wen there is no ref in props', () => {
const props = { x: 'y' };
const vnode = jsx('div', props);
expect(vnode.props).to.equal(props);
});

it('should add keys', () => {
Expand Down

0 comments on commit f3edc90

Please sign in to comment.