DEV Community

Discussion on: 3 ways to clone an object in Javascript

Collapse
 
nithish_13 profile image
Nithish Kumar • Edited

There are lot of reasons . The main disadvantage is , JSON.stringify() converts Date objects to string. If the value of the key is undefined, when doing with JSON.stringify(), the key is lost. For example,

let obj1 = {
  date : new Date(),
  name : undefined
}
console.log(JSON.parse(JSON.stringify(obj1))  // would return {date: "2021-05..." }
Enter fullscreen mode Exit fullscreen mode

Notice that the name attribute(or key) is lost.
Also,, it causes performance issue. So developers prefer either JQuery or lodash to implement deep copy.