DEV Community

Cover image for Deep Copy in Node JS without using any external packages
Shuvo
Shuvo

Posted on

3 2

Deep Copy in Node JS without using any external packages

Using v8 on Noe JS you can deep copy object without using any external packages. Here is an example

const v8 = require("v8")

const obj = {
    status: "verified",
    profile: {
        name: "John Doe",
        email: "john@gmail.com",
        phone: "123-456-7890"
    }
}

const deepCopiedObj = v8.deserialize(v8.serialize(obj))
Enter fullscreen mode Exit fullscreen mode

Now this looks similar to JSON.parse(JSON.stringify(obj)) but the internal workings are different.

Top comments (4)

Collapse
 
hidaytrahman profile image
Hidayt Rahman

Is v8 is external package?

Collapse
 
0shuvo0 profile image
Shuvo

No it's a core package of nodejs

Collapse
 
dylanwatsonsoftware profile image
Dylan Watson

So.... Why not just use JSON.parse(JSON.stringify(obj)) then?

Collapse
 
0shuvo0 profile image
Shuvo

Their working are different

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay