In every programming language sometimes there is a need to make an exact copy of one object. Copy that will be a faithful reflection of the base object, with all attributes and fields at every nesting level. In JavaScript we have several ways to make a deep clone of an object.
2. Using JSON stringify function
Note that this method will not work if your object contains: Dates, functions, undefined, Infinity, RegExps, Maps, Sets, Blobs, FileLists, ImageDatas, sparse Arrays, Typed Arrays or other complex types.
3. Using lodash deep clone method
4. Angular framework comes with angular.copy function
5. Using jQuery extend function
6. Using Object.assign (this makes a shallow copy, thanks Concerned Citizen for pointing this out in comments)
7. Performance comparision
Now lets check the performance comparision.
If you want to check this benchmark by yourself, click this link: http://jsben.ch/P8MHg
8. Conclusion
According to the benchmark test, the fastest way to deep clone an object in javascript is to use lodash deep clone function since Object.assign supports only shallow copy. Unfortunately, this is an external library, if you don't want to use one you can check below deepClone function written in vanilla JS.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}