JavaScript · Middle · Question 31 of 75
Why is structuredClone(obj) better than JSON.parse(JSON.stringify(obj))?
- ✓Correct answer: It keeps
Date,Map,Set, circular references and doesn't loseundefined - 2It also copies functions and class methods along with the prototype
- 3It works only with primitives but is dozens of times faster
Explanation
The JSON trick turns Date into a string, loses undefined, Map, Set, Infinity and fails on cycles. structuredClone supports more types but doesn't copy functions, DOM nodes or class prototypes - such objects throw or lose their methods.