3 ms·
Deep-copying in JavaScript using structuredClone
- mthoms 5y agoThis is sorely needed and a welcome addition. Tangent: What are they referring to when they write "The Platform" (capitalized) as in "The Platform now ships with ....". It sounds a little too Borg-like and creepy.
- petercooper 5y agoThe "(Open) Web Platform" perhaps? A term that numerous groups like the W3C were trying to push hard but that doesn't seem to have hugely caught on in day to day conversation.
- peanut_worm 5y agoThis is very useful and definitely something I am glad to see added to the language, previous solutions to this problem weren’t particularly efficient. I am confused why they made it a global function though rather than a static method like object.assign though also > Functions: If your object contains functions, they will be quiety discarded. that is a bit of a let down
- aliswe 5y agoi believe thats for the hack, stringifying then parsing the object.
- peanut_worm 5y agoOops I can’t read
- electroly 5y agoI think it's true for both -- they mention this both under the "Deep copies" section where it's talking about JSON.parse(JSON.stringify()), and also under "Structured cloning" > "Features and limitations" where it's talking about the new function. The line GP quoted is from the section where it's talking about structured cloning.
- olliej 5y agoSerializing functions is an exciting footgun of pain due to capturing/closures. Sure there are plenty of places where you could serialize easily, it’s super easy to become nonserializable and you end up in a position where sometime a function serializes, and sometimes it fails. Then there are semantic issues in later execution as a lot of primitive operations are defined in terms of the host global object, etc. So the safest option is to not support serialization.
- ijelliti 5y agoAwesome right in time
- starklevnertz 5y agoI found that if I’m deep copying then that’s a sign my software design is wrong. There’s a lot of challenges and problems with deep copying, better to find another way.
- aliswe 5y ago> The Platform now ships with structuredClone() ... What's "The Platform"?
- typingmonkey 5y agoWhenever I research the performance problem of a JavaScript library, I stumble over many deep clones even when it is not necessary most of the time. People are just not aware how CPU expensive a deep clone in JavaScript is, even more on big objects. For example when you run a Node.JS profiling on the PouchDB test suite, most CPU cycles are spend in running the clone() function. Having an updated performance comparison of structuredClone() would be great.