Skip to content

fix: [#2045] Replace JSON.parse(JSON.stringify()) with structuredClone()#2048

Closed
TrevorBurnham wants to merge 1 commit intocapricorn86:masterfrom
TrevorBurnham:fix/structuredClone-optimization
Closed

fix: [#2045] Replace JSON.parse(JSON.stringify()) with structuredClone()#2048
TrevorBurnham wants to merge 1 commit intocapricorn86:masterfrom
TrevorBurnham:fix/structuredClone-optimization

Conversation

@TrevorBurnham
Copy link
Copy Markdown
Contributor

Resolves #2045

This PR uses structuredClone() for deep cloning instead of JSON.parse(JSON.stringify()).

Files changed

  • Headers.ts: Clone entries when constructing from another Headers instance
  • CSSStyleDeclarationPropertyManager.ts: Clone properties in clone() method
  • MediaStreamTrack.ts: Clone CAPABILITIES and SETTINGS constants

@TrevorBurnham
Copy link
Copy Markdown
Contributor Author

I believe the CI failures are due to #2022, which is fixed by #2023.

@capricorn86
Copy link
Copy Markdown
Owner

Is structuredClone() really faster?

Many benchmarks suggests that JSON.stringify() is much faster for certain objects as it doesn't have to round-trip to the JavaScript engine.

Here are some benchmarks:
https://www.measurethat.net/Benchmarks/Show/18541/0/jsonstringify-vs-structuredclone#latest_results_block

Would be interesting to benchmark with the actual code in Happy DOM.

@TrevorBurnham
Copy link
Copy Markdown
Contributor Author

Huh! I'm surprised, but my own checks back up those benchmarks. Hopefully future Node.js versions will improve structuredClone performance. Until then, closing this out.

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

Successfully merging this pull request may close these issues.

Performance: Use structuredClone instead of JSON.parse(JSON.stringify(obj))

2 participants