Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize createElement #27271

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions packages/react/src/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,10 @@ function ReactElement(type, key, ref, self, source, owner, props) {
writable: false,
value: source,
});
if (Object.freeze) {
Object.freeze(element.props);
Object.freeze(element);
}

Object.freeze(element);
}
Object.freeze(element.props);

return element;
}
Expand Down Expand Up @@ -363,17 +362,18 @@ export function createElement(type, config, children) {
let propName;

// Reserved names are extracted
const props = {};
let props = {};

let key = null;
let ref = null;
let self = null;
let source = null;

let allowPropsToReferenceConfig = true;

if (config != null) {
if (hasValidRef(config)) {
ref = config.ref;

if (__DEV__) {
warnIfStringRefCannotBeAutoConverted(config);
}
Expand All @@ -387,13 +387,28 @@ export function createElement(type, config, children) {

self = config.__self === undefined ? null : config.__self;
source = config.__source === undefined ? null : config.__source;
// Remaining properties are added to a new props object
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
props[propName] = config[propName];

if (
Object.isFrozen(config) ||
hasOwnProperty.call(config, 'key') ||
hasOwnProperty.call(config, 'ref') ||
hasOwnProperty.call(config, '__self') ||
hasOwnProperty.call(config, '__source')
) {
allowPropsToReferenceConfig = false;
}

if (allowPropsToReferenceConfig) {
props = config;
} else {
// Remaining properties are added to a new props object
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
props[propName] = config[propName];
}
}
}
}
Expand Down
40 changes: 12 additions & 28 deletions packages/react/src/__tests__/ReactElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ describe('ReactElement', () => {
const config = {foo: 1};
const element = React.createElement(ComponentClass, config);
expect(element.props.foo).toBe(1);
config.foo = 2;
expect(() => (config.foo = 2)).toThrow();

expect(element.props.foo).toBe(1);
});

Expand All @@ -150,8 +151,9 @@ describe('ReactElement', () => {
expect(element.ref).toBe('34');
if (__DEV__) {
expect(Object.isFrozen(element)).toBe(true);
expect(Object.isFrozen(element.props)).toBe(true);
}
expect(Object.isFrozen(element.props)).toBe(true);

expect(element.props).toEqual({foo: '56'});
});

Expand Down Expand Up @@ -333,25 +335,16 @@ describe('ReactElement', () => {
render() {
const el = <div className="moo" />;

if (__DEV__) {
expect(function () {
el.props.className = 'quack';
}).toThrow();
expect(el.props.className).toBe('moo');
} else {
expect(function () {
el.props.className = 'quack';
expect(el.props.className).toBe('quack');
}
}).toThrow();
expect(el.props.className).toBe('moo');

return el;
}
}
const outer = ReactTestUtils.renderIntoDocument(<Outer color="orange" />);
if (__DEV__) {
expect(ReactDOM.findDOMNode(outer).className).toBe('moo');
} else {
expect(ReactDOM.findDOMNode(outer).className).toBe('quack');
}
expect(ReactDOM.findDOMNode(outer).className).toBe('moo');
});

it('throws when adding a prop (in dev) after element creation', () => {
Expand All @@ -360,27 +353,18 @@ describe('ReactElement', () => {
render() {
const el = <div>{this.props.sound}</div>;

if (__DEV__) {
expect(function () {
el.props.className = 'quack';
}).toThrow();
expect(el.props.className).toBe(undefined);
} else {
expect(function () {
el.props.className = 'quack';
expect(el.props.className).toBe('quack');
}
}).toThrow();
expect(el.props.className).toBe(undefined);

return el;
}
}
Outer.defaultProps = {sound: 'meow'};
const outer = ReactDOM.render(<Outer />, container);
expect(ReactDOM.findDOMNode(outer).textContent).toBe('meow');
if (__DEV__) {
expect(ReactDOM.findDOMNode(outer).className).toBe('');
} else {
expect(ReactDOM.findDOMNode(outer).className).toBe('quack');
}
expect(ReactDOM.findDOMNode(outer).className).toBe('');
});

it('does not warn for NaN props', () => {
Expand Down