You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why does attempting to extend the native String prototype impact the performance of the parser?
I'm less concerned about mitigating the issue, since we can probably avoid extending the String prototype. I'm mostly curious as to how this is even possible 😕.
"use strict";constparse5=require("parse5");functionbench(){constA=performance.now();consthtml=`<doctype html><html><head><title>Test</title></head><body><h1>Hello World</h1></body></html>`;for(leti=0;i<100_000;++i){constdoc=parse5.parse(html);}constB=performance.now();console.log("Done",B-A);}bench();Object.create(String.prototype);// this line shouldn't do anythingbench();
In this example, the line Object.create(String.prototype); should not do anything, since by description this is a read operation of the String prototype. However, leaving this line in causes the second run of bench() to take 20% to 30% longer.
Without the line:
Done 558.624419927597
Done 513.1717121601105 // second run might be faster due to optimization, that is fine.
With the line:
Done 559.1178510189056
Done 694.2957258224487 // second run gets significantly slower
The text was updated successfully, but these errors were encountered:
After some more testing it seems that Object.create(String.prototype) simply de-optimizes string operations in general, and not something specific to parse5. Closing.
Reported to my team in aws/aws-sdk-js-v3#6182.
Using v7.2.1 of parse5. Node.js v20.5.1.
Why does attempting to extend the native String prototype impact the performance of the parser?
I'm less concerned about mitigating the issue, since we can probably avoid extending the String prototype. I'm mostly curious as to how this is even possible 😕.
In this example, the line
Object.create(String.prototype);
should not do anything, since by description this is a read operation of the String prototype. However, leaving this line in causes the second run ofbench()
to take 20% to 30% longer.Without the line:
With the line:
The text was updated successfully, but these errors were encountered: