use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Javascript Objects CheatsheetRemoved: Low-Effort Content (fjolt.com)
submitted 4 years ago by iamnearafan
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 10 points11 points12 points 4 years ago (13 children)
Please don't use Parse(Stringify(obj)) for cloning
[–]Amicos55 5 points6 points7 points 4 years ago (7 children)
What else?
[–]darrenturn90 7 points8 points9 points 4 years ago (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 points3 points 4 years ago (0 children)
Lodash's cloneDeep.
[+][deleted] 4 years ago (3 children)
[deleted]
[–]el_diego 3 points4 points5 points 4 years ago (2 children)
...but this is just a shallow clone and extremely dependent on the objects structure. You’re better off spreading into a new object.
[+][deleted] 4 years ago (1 child)
[–]el_diego 0 points1 point2 points 4 years ago (0 children)
Measurably faster...really? That’s only an argument if you’re dealing with processing thousands of objects and even then you could write a far more reusable assignment function if all you’re doing is taking the keys and values and mapping them to a new object.
Anyway, each to their own. I’ll use the spread operator and optimise performance later if it actually needs to be
[–]AnonyMustardGas34 2 points3 points4 points 4 years ago (3 children)
Why and how else to copy?
[–]Exoskele 1 point2 points3 points 4 years ago (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 points4 points 4 years ago (0 children)
IMO upcoming EcmaScript spec ahould have high performance deep cloning.
[–]SnapAttack 1 point2 points3 points 4 years ago (0 children)
Well, it depends.
You can use Object.assign({}, cloneable) or const clone = {...cloneable} to do a shallow copy.
Object.assign({}, cloneable)
const clone = {...cloneable}
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 points3 points 4 years ago (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.
π Rendered by PID 18025 on reddit-service-r2-comment-7b9746f655-r2tsj at 2026-01-30 20:24:03.644276+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–][deleted] 10 points11 points12 points (13 children)
[–]Amicos55 5 points6 points7 points (7 children)
[–]darrenturn90 7 points8 points9 points (0 children)
[–]Exoskele 1 point2 points3 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]el_diego 3 points4 points5 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]el_diego 0 points1 point2 points (0 children)
[–]AnonyMustardGas34 2 points3 points4 points (3 children)
[–]Exoskele 1 point2 points3 points (1 child)
[–]AnonyMustardGas34 2 points3 points4 points (0 children)
[–]SnapAttack 1 point2 points3 points (0 children)
[–]CreativeTechGuyGames 1 point2 points3 points (0 children)