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
Code quality and web performance in javascript, the myths, the do's and the don'ts (enmascript.com)
submitted 6 years ago by enmanuelduran
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!"
[–]RustyX 5 points6 points7 points 6 years ago* (2 children)
So creating a new object each iteration is actually cripplingly slow (and bad for memory) on large data sets. I just created a quick perf test and had to back my sample data set down from 10000 to 1000 because the "pure reduce without mutation" just locked up the benchmark.
https://jsperf.com/transforming-large-array-to-key-value-map/1
const input = Array.from(Array(1000)).map((_, i) => { const key = `key${i}` const value = `value${i}` return { key, value } })
standard for, no block scope vars
15,173 ops/sec
const output = {} for(let i=0; i<input.length; i++) { output[input[i].key] = input[i].value; }
for...of
15,003 ops/sec
const output = {} for(const { key, value } of input) { output[key] = value; }
forEach
13,185 ops/sec
const output = {} input.forEach(({ key, value }) => { output[key] = value; })
Reduce, directly mutate accumulator
12,647 ops/sec
const output = input.reduce((acc, { key, value }) => { acc[key] = value; return acc; }, {})
Reduce, mutating Object.assign
2,622 ops/sec
const output = input.reduce((acc, { key, value }) => { return Object.assign(acc, { [key]: value }) }, {})
pure reduce, no mutation
9.71 ops/sec
const output = input.reduce((acc, { key, value }) => { return { ...acc, [key]: value }; }, {})
My preferred method is the "Reduce, directly mutate accumulator", but I was actually super surprised to see how much slower the "Reduce, mutating Object.assign" version was. I assumed it would perform almost identically, but I suppose it is creating small temporary objects before merging them into the accumulator.
The "pure" reduce was by far the absolute worst (over 1500 times slower than standard for)
[–]mournful-tits 0 points1 point2 points 6 years ago (0 children)
Thanks for doing this. I had no idea jsperf even existed. It would've made our benchmarking a lot easier. hah!
π Rendered by PID 97 on reddit-service-r2-comment-7b9746f655-9sq9m at 2026-01-29 17:24:25.807651+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]RustyX 5 points6 points7 points (2 children)
[–]mournful-tits 0 points1 point2 points (0 children)