DEV Community

Discussion on: What's the best way to deeply clone an object in JavaScript?

Collapse
 
penguinsource profile image
Mihai

If it's not a nested object, it's really nice to use the spread operator
const newObject = { ..currentObject };

If it's a nested or deeply nested object, it's much safer to use
const newObject = JSON.parse(JSON.stringify(currentObject))

in a nutshell:
const cloneDeep = (obj) => {
return JSON.parse(JSON.stringify(obj))
}

I should say i haven't taken the time to see any performance detriments, if any, appear.