Deep Copy in Node JS without using any external packages

Shuvo - Oct 19 '21 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .