Performance issues for `getElementById`
Description
The Document.prototype.getElementById method that shadydom attaches to the document is much slower than the built in version. We noticed this on a customers site when we added the webcomponents/webcomponentsjs polyfill to the page, where the page load time went from ~4-5s to ~25-30s in IE 11. The cause of this is that the customers site is not very optimised and made roughly 2200 calls to document.getElementById().
The small demo I made shows that it takes ~0-1ms to query for id 1000 times, while it takes ~1200ms doing the same in IE 11. Even in Chrome this takes roughly 200ms, which is a lot slower.
Issue #155 might be related, but I think something might be done to mitigate this specific issue.
I haven't looked into the current code too much, but it didn't look like the native getElementById method was used. Would it be possible to first use the result from the native method, if it does not have a shadowRoot parent? It would probably cover a lot of cases and avoid having to manually traverse and find the id.
Live Demo
http://jsbin.com/fidaqarozu/1/edit?html
Press Native or ShadyDOM buttons to run lots of getElementById and measure the time.
Steps to Reproduce
- Include the webcomponents polyfill
- Use
document.getElementById()a little too much
Expected Results
Slower, but not a brutal performance impact to use document.getElementById()
Actual Results
Using document.getElementById() is much much slower, even when no shadowRoots are in use.
Browsers Affected
All browsers not shipping native ShadowDOM (e.g. shadydom polyfill is used). Older/slower browsers is affected worse.
Versions
- webcomponents: v2.0.0
We have had a similar measure for querySelector as added in #216. We can add the option of useNative, however as pointed out in https://github.com/webcomponents/shadydom/pull/210#issuecomment-361435503 it misses distributed nodes. We have to consider the impact of this before exposing the native variant.
@TimvdLippe Thanks for the info. Though, not sure what undistributed elements/nodes refers to exactly so you have way better insight there than me.
The performance issue is not urgent at the moment ifor us. We have a workaround where we restore the native getElementById method. This only works because we are not using id's in our components, so our conclusion was that the shadydom id method was not required in our case.
I created this issue mostly because the large performance gap was unexpected, and it took some time to figure out it was the ShadyDOM polyfill and document.getElementById() that was the cause (the dev tools in IE11 takes quite a bit of time to work with). Maybe the known issues section on webcomponentsjs could have a small section about performance impact? Since this is true by just including the polyfills, even if no ShadowDOM or custom elements are currently used.
@csvn Distributed nodes are nodes that moved in the DOM tree per the <slot> semantics. E.g. while the node is placed in the light DOM of the one element, in the polyfill it is moved to match the correct location. Since we are thus moving nodes, selection mechanisms like querySelector and getElementById would no longer match nodes that have been moved.
I would like to explore additional opportunities of addressing the performance and only if we are absolutely unable to add it to the list of known issues. The known issues list consist of issues that we are not going to address simply because we can not do so.
@TimvdLippe Makes sense, thanks for the explanation.
I get it, putting performance on known issues can scare away users even if there are no big performance impact in the majority of cases. Let me know if you need anything else, otherwise I'll just await your decisions 👍
@csvn Could you please mention how you restored the native getElementById method?
@parit Of course! My approach is not very pretty, but it works. It's just capturing the native version of getElementById() before importing the shadydom polyfill, and restoring it after.
// polyfills.js
import { restoreNativeGetElementById } from './shadydom-polyfill-fix';
import '@webcomponents/shadydom/shadydom.min.js';
// or import '@webcomponents/webcomponentsjs';
// `shadydom` replaces `Document.prototype.getElementById` with a custom method.
// This method is much slower than the original. Since we are not using `id`'s in our code,
// having the native `getElementById` on Document should not affect us
restoreNativeGetElementById();
// shadydom-polyfill-fix.js
const nativeGetElementById = Document.prototype.getElementById;
export function restoreNativeGetElementById() {
Document.prototype.getElementById = nativeGetElementById;
}