Skip to content

Update helpers.ts fix for #134#136

Open
pabx06 wants to merge 2 commits intorelative:masterfrom
pabx06:issue-134
Open

Update helpers.ts fix for #134#136
pabx06 wants to merge 2 commits intorelative:masterfrom
pabx06:issue-134

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