Use object bracket access test for RESERVED_PROPS#12530
Use object bracket access test for RESERVED_PROPS#12530Deraen wants to merge 1 commit intofacebook:masterfrom
Conversation
Details of bundled changes.Comparing: 00a0e3c...cb8923d react-is
Generated by 🚫 dangerJS |
- Should be faster - Google Closure Compiler will optimize away static properties from objects if properties aren't accessed dynamically. Using bracket access will make Closure to keep these properties in optimized code.
| let markup = null; | ||
| if (isCustomComponent(tagLowercase, props)) { | ||
| if (!RESERVED_PROPS.hasOwnProperty(propKey)) { | ||
| if (!RESERVED_PROPS[propKey]) { |
There was a problem hiding this comment.
I feel hesitant because there might've been some reason it was written this way. For example I think this will have a false positive for hasOwnProperty. And potentially will make adding anything to Object prototype in JS spec a breaking change.
There was a problem hiding this comment.
Oh right. I forgot how JS objects work.
I think I'm out of ideas for fixing #12368 here. I'll check on Closure side if it can be made to understand hasOwnProperty as dynamic access so it can keep the properties like with bracket access.
There was a problem hiding this comment.
Could you give me more details on your strategy in general? Is this the only issue blocking you? It's a bit hard to believe given in how many places we use dot-access.
Continuing from #12370 with different solution (ping @gaearon)
hasOwnProperty(if my benchmark works correctly anyway)objects if properties aren't accessed dynamically. Using bracket access
will make Closure to keep these properties in optimized code.
I thought
preserveTypeAnnotationswould keep all JSDoc annotations but@nocollapseseems to be different than "general" JSDoc comments, so it is not kept.After failure with that approach I started looking into why https://github.com/facebook/react/blob/master/packages/shared/isTextInputElement.js#L13-L29 works with Closure optimizations (with externs to keep the property names). The function is transpiled into static object and a function which uses brackets to access the object. Turns out, that because the code is using brackets to access the object Closure doesn't optimize the properties away.
I checked performance difference between hasOwnProperty and brackets, and looks like object bracket access is faster: https://jsperf.com/static-object-check-if-prop-exists/1