Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I would never deep copy using any of those methods. Can't you just write your own recursive version? I remember plenty of examples on jsPerf (site not working at the moment) including my own that significantly outperformed JSON parse/stringify.



Yeah I'm confused as most of those methods, like postMessage, stringifies things anyway. I simply iterate other object properties to make a copy of a full object.

I've never run performance tests but surely that's faster than parsing an object into its string representation then parsing it back into the language, no?


As I explain in the article, postMessage does not stringify. It runs the structural clone algorithm, as per spec. Otherwise things like ArrayBuffers, Sets et al. would not make it to the other side.


The problem is figuring out if you're in a looping/cycling data structure or not. JSON.stringify and friends often already have code to deal with that, and it's quite complex in pure JS because there is no "object id" function.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: