Avoid deoptimization by not accessing undefined properties and/or initializing null properties
For fun I ran these benchmarks (https://github.com/paulshen/react-bench/blob/gh-pages/tests.js) on serverside by using React.renderComponentToString().
For example, node --trace_deopt sometestfile.js shows that ReactComponent.isValidComponent() is optimized and deoptimized hundreds of times (depending on benchmark iterations, obviously), due to accessing object.mountComponentIntoNode, which is undefined for most leaf nodes of type string or number. So a quick fix for isValidComponent() is to check typeof object === 'object' first. It also avoids some other cascading deopts elsewhere.
Thousands of other undefined accesses can be seen in v8.log after running with node --log_suspect sometestfile.js. Accessing undefined properties is perhaps the most common deopt reason (among with adding new properties without initialising them as null or something earlier), so perhaps they should be fixed for speed/memory performance (optimized functions use less intermediate variables, so less gc burden I suppose).
For example __reactAutoBindMap is accessed as undefined in ReactCompositeComponentMixin.mountComponent(), so it could be either initialized as null somewhere, or one could prevent undefined access by using if ('__reactAutoBindMap' in this) { instead of if (this.__reactAutoBindMap) {. Similarly for __keyValidated__ property, that is accessed as undefined in validateExplicitKey().
I haven't really looked at the output of node --trace_deopt --trace_opt_verbose sometestfile.js, it could reveal some optimization possibilities too.
Thank you for bringing this up. We try to do a good job of setting default properties/members in the base classes to avoid this issue, but as you said - the objects we're checking aren't always React components - they're strings and numbers that we're detecting as text etc. Great idea about first checking typeof x == 'object'! Let's start making --trace_deopt part of our regular test plan for core diffs that add new members.
ReactComponent.isValidComponent() deoptimization is mostly due to validateChildKeys() which is DEV only, so it has not affected prod builds. Also it seems that Firefox etc deal with undefined member access very gracefully, so generally this may not be as important as seems to be from node/V8 point of view.
http://jsfiddle.net/tiagorg/5L9qxnsq/ any suggestions how fix it