Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending String.prototype impacts parse5.parse performance #1348

Closed
kuhe opened this issue Dec 6, 2024 · 1 comment
Closed

Extending String.prototype impacts parse5.parse performance #1348

kuhe opened this issue Dec 6, 2024 · 1 comment

Comments

@kuhe
Copy link

kuhe commented Dec 6, 2024

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 😕.

"use strict";
const parse5 = require("parse5");

function bench() {
  const A = performance.now();

  const html = `<doctype html><html><head><title>Test</title></head><body><h1>Hello World</h1></body></html>`;

  for (let i = 0; i < 100_000; ++i) {
    const doc = parse5.parse(html);
  }

  const B = performance.now();

  console.log("Done", B - A);
}

bench();
Object.create(String.prototype); // this line shouldn't do anything
bench();

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
@kuhe
Copy link
Author

kuhe commented Dec 6, 2024

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.

@kuhe kuhe closed this as completed Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant