Hi Tuan Tran, thanks for stopping by and your question about
I updated the article to add 3 more methods of deep copy: a custom function,
JSON.stringify, and the
rfdc (Really Fast Deep Clone) library.
The usual methods of copying an object or array only make a shallow copy, so deeply-nested references are a problem…
Kevin Dalman correctly points out that
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.stringify() in the updated article on how to deep copy, so please take a look!
Alexis Chappron is definitely right that
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
Alexis also pointed out that
stringify will fail for circular references, though
rfdc is the only deep copy method I cover that expects circular references with an optional flag.