DOM access is inherently megamorphic. This perf-tests attempts to prove that caching the methods on the DOM and invoking them using .call() is faster than using the megamorphic property lookup.
// Cache the getter only once. const Node_parentNode_getter = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode').get; Node_parentNode_getter.call(domNode); // Is the fastest way to access DOM. domNode.parentNode; // is megamorphic and hence slower than the line above.
This approach seems to hold in all browser (except for chrome due to issue 8820).
Further reading:
profiling...
Browser | OBJ_mega/OBJ_mono | OBJ_call/OBJ_mono | DOM_mega/DOM_mono | DOM_call/DOM_mono |
---|---|---|---|---|
computing... | computing... | computing... | computing... | computing... |
-- historic for comparison data -- | ||||
Chrome/72.0.3626.96 Safari/537.36 | 181.7 | 5.2 | 2.9 | 3.5 |
Gecko/20100101 Firefox/64.0 | 13 | 0.6 | 6.6 | 1.2 |
Version/12.2 Safari/605.1.15 | 24.4 | 2 | 9.1 | 3 |