正在加载视频...

视频加载失败

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,299 次观看 • 3 年前 •via X (Twitter)

10 条评论

Steve (Builder.io) 的头像
Steve (Builder.io)3 年前

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

Luke Edwards 的头像
Luke Edwards3 年前

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 的头像
Jarred Sumner3 年前

ugh we still need to implement this in bun

Jesse Pence 的头像
Jesse Pence3 年前

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) 的头像
Steve (Builder.io)3 年前

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 的头像
Isaac Way3 年前

Js has the most random standard library

Okiki Ojo 的头像
Okiki Ojo3 年前

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 的头像
Conny Brunnkvist3 年前

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

RodAvi 的头像
RodAvi3 年前

Awesome tip Thanks !!!!

Tomas Pi Puig 的头像
Tomas Pi Puig3 年前

Actually not bad..

相关视频