Hi Tuan Tran, thanks for stopping by and your question about JSON.parse()
and JSON.stringify()
.
I updated the article to add 3 more methods of deep copy: a custom function, JSON.parse
/JSON.stringify
, and the rfdc
(Really Fast Deep Clone) library.
Kevin Dalman correctly points out that JSON.parse()
and JSON.stringify()
only work for nested objects and arrays that contain only primitive values, objects, and arrays — Date and RegExp objects are examples that will not work correctly.
I demonstrate some of these examples of JSON.parse()
and JSON.stringify()
in the updated article on how to deep copy, so please take a look!
Alexis Chappron is definitely right that parse
& stringify
can be slower than a custom function or rfdc
, though it will generally be faster (for the data types it works with) than lodash’s _.cloneDeep
functionality.
Alexis also pointed out that parse
& stringify
will fail for circular references, though rfdc
is the only deep copy method I cover that expects circular references with an optional flag.