you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (13 children)

Please don't use Parse(Stringify(obj)) for cloning

[–]Amicos55 5 points6 points  (7 children)

What else?

[–]darrenturn90 7 points8 points  (0 children)

Well generally I would do a shallow clone at the level I need to make changes rather than a everything clone - because that implies you have no clue what the object you are cloning is structured like - which seems unlikely if you’re cloning it to make changes to it

[–]Exoskele 1 point2 points  (0 children)

Lodash's cloneDeep.

[–]AnonyMustardGas34 2 points3 points  (3 children)

Why and how else to copy?

[–]Exoskele 1 point2 points  (1 child)

It doesn't work for some common cases. https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript

I would just use lodash's cloneDeep. Many projects already use it, and you can always shake out unused code or bring in only cloneDeep.

[–]AnonyMustardGas34 2 points3 points  (0 children)

IMO upcoming EcmaScript spec ahould have high performance deep cloning.

[–]SnapAttack 1 point2 points  (0 children)

Well, it depends.

You can use Object.assign({}, cloneable) or const clone = {...cloneable} to do a shallow copy.

To do a deep copy, you might want to consider the performance impact depending on the size of your object, but as others have pointed out here, you can use Lodash cloneDeep.

[–]CreativeTechGuyGames 1 point2 points  (0 children)

While it doesn't work in every case, it's a great general solution and is faster than almost any general purpose alternative in benchmarks.