JSON.serialize
and then JSON.parse
on a value is the fastest general-purpose way to deeply clone an object in Javascript. However, the downside is that it only works on values for which x => JSON.parse(JSON.serialize(x))
is the identity function. In other words, this function will only work correctly on strings, numbers, booleans, null
, undefined
, and arrays and objects built off of these types.