Loading video...

Video Failed to Load

Go Home

This is one you need to know. JS has a built-in function for deep cloning objects called `structuredClone`, which can handle: 😍 Deeply nested objects and arrays 🌎 Many types, like Date, Set, Map, etc ➰ Circular references More in my latest article:

551,315 views • 3 years ago •via X (Twitter)

10 Comments

Steve (Builder.io)'s profile picture
Steve (Builder.io)3 years ago

More on what `structuredClone` can do, and how it compares to alternatives, in my latest post:

Luke Edwards's profile picture
Luke Edwards3 years ago

Unfortunately it’s comparatively pretty slow & relatively new so a significant portion of audiences can’t support it. is 500b at heaviest and extremely fast.

Jarred Sumner's profile picture
Jarred Sumner3 years ago

ugh we still need to implement this in bun

Jesse Pence's profile picture
Jesse Pence3 years ago

I see the double JSON move in so many old code bases. Would this be worthy of a pull request or would I probably just annoy the maintainers?

Steve (Builder.io)'s profile picture
Steve (Builder.io)3 years ago

Good Q. There are some minor but notable differences, like JSON is a little faster in some cases, and structureClone will throw errors for some things JSON won’t and visa versa, and handling some types like undefined are different. Probably safest to not semi blindly swap

Isaac Way's profile picture
Isaac Way3 years ago

Js has the most random standard library

Okiki Ojo's profile picture
Okiki Ojo3 years ago

structuredClone also supports ArrayBuffers, Streams, etc... but, unfortunately you have to manually list them out as transferable objects for it to work, that's where `transferables` comes in, it makes it fast and easy to use transferable objects

Conny Brunnkvist's profile picture
Conny Brunnkvist3 years ago

Thanks for highlighting this. Reading up, I found these note on its limitations interesting too:

RodAvi's profile picture
RodAvi3 years ago

Awesome tip Thanks !!!!

Tomas Pi Puig's profile picture
Tomas Pi Puig3 years ago

Actually not bad..

Related Videos