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

Update helpers.ts fix for #134 #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

pabx06
Copy link

@pabx06 pabx06 commented Dec 26, 2023

fix for #134
JSON.parse(JSON.stringify(item)) have trouble on large object

@relative
Copy link
Owner

the spread operator won't work here since it only clones the top level keys

@j4k0xb
Copy link

j4k0xb commented Dec 26, 2023

maybe the code can be refactored to avoid cloning altogether.
like here it shouldn't make a difference if it uses the original node or a copy:

ForStatement(node) {
if (Guard.isBlockStatement(node.body)) return
sp<BlockStatement>(node.body, {
type: 'BlockStatement',
body: [immutate(node.body)],
})
},

the only place where it might be necessary is related to control flow functions

@pabx06
Copy link
Author

pabx06 commented Dec 27, 2023

Pretty sure this one is better

-native deep clone:
structuredClone:
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone

immutable to deep clone using structuredClone
@j4k0xb
Copy link

j4k0xb commented Dec 27, 2023

based on my tests structuredClone is a fair bit slower than json parse/stringify, but as long as it fixes the issue it shouldn't matter too much
but there are some significantly faster alternatives that could also be used: https://github.com/lukeed/klona/blob/master/src/json.js (benchmark), can even improve the perf by another 25% by not checking for __proto__

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.

3 participants