Optimize reading innerHTML for no-replacement cases.
In the case that EntityMask is 0 we don't even need to iterate the characters and can just call Append(const StringView&) which will share the underlying StringImpl if the builder is empty. This optimizes the case where someone does something like: <script id=json type=json>{huge blob of text}</script> and then JSON.parse(document.getElementById('json').innerHTML) which would previously iterate the string doing nothing and then make a copy. While technically using .textContent would be the fast path devs don't seem to often do that. For example see this twitter thread: https://twitter.com/ElliottZ/status/1171817105336832000?s=20 and also many other places like SO posts: https://stackoverflow.com/questions/7581133/how-can-i-read-a-json-in-the-script-tag-from-javascript/7956249#7956249 as well as inside Airbnb's codebase (which I fixed to .textContent). I went further and optimized the case where we have a non-empty EntityMask, but don't replace anything. For example if you have a large paragraph of text and do getElementById('p').innerHTML in many cases there's nothing to replace. We can't avoid the O(n) scan of the string in that case, but we can at least avoid the copy. Change-Id: I4270b7d8f693fe8dcf8f95271f33202a209ea298 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2366612Reviewed-by:Ian Kilpatrick <ikilpatrick@chromium.org> Reviewed-by:
Yoshifumi Inoue <yosin@chromium.org> Commit-Queue: Elliott Sprehn <esprehn@chromium.org> Cr-Commit-Position: refs/heads/master@{#800450}
Showing
Please register or sign in to comment