r/learnjavascript • • Jul 22 '26

Performance Trap, some benchmarks and... subjective taste

I've done some benchmarks. ES5 vs ES6. What do you prefer? I ask this because I have often seen in other people's codes, where performance, optimization was required, ES6 code that decreased performance, which also had a negative impact on UI/UX.

This is more about the syntax used and not about which is faster.

Array size: 1,000,000 | Runs per test: 15

1. TRANSFORM — double 1,000,000 numbers

TestAvg (ms)Min (ms)Max (ms)
for loop (var, ES5)0.7330.61
array.map5.4474.76.9
forEach5.965.46.8
for...of (ES6)7.065.411.1

for loop (var, ES5) beat for...of (ES6) by 9.63x

2. FILTER — keep evens out of 1,000,000

TestAvg (ms)Min (ms)Max (ms)
for loop + push (ES5)2.2871.63.4
array.filter (ES6)6.065.27.2

for loop + push (ES5) beat array.filter (ES6) by 2.65x

3. SUM — add up 1,000,000 numbers

TestAvg (ms)Min (ms)Max (ms)
for loop (var, ES5)0.760.61.1
for...of3.8333.74.4
array.reduce4.5734.44.7

for loop (var, ES5) beat array.reduce by 6.02x

4. LOOKUP — find one value (worst case)

TestAvg (ms)Min (ms)Max (ms)
Set.has — O(1)000
array.includes — O(n)0.07300.1
array.indexOf !== -1 — O(n)0.07300.2

Set.has — O(1) beat array.indexOf !== -1 — O(n)

5. STRINGS — build 200,000 strings

TestAvg (ms)Min (ms)Max (ms)
concatenation "+"3.8873.37.8
template literal ${}3.93.54.3

concatenation "+" beat template literal ${} by 1.00x

6. OBJECT ITERATION — sum 50,000 values

TestAvg (ms)Min (ms)Max (ms)
for...in4.27344.6
Object.keys + forEach4.384.24.7
Object.values + reduce6.569.2

for...in beat Object.values + reduce by 1.52x

7. ARRAY CREATION — build 1,000,000 squares

TestAvg (ms)Min (ms)Max (ms)
for loop + push (ES5)8.1736.523.4
new Array(n).fill(0).map()17.3942.4
Array.from({length}, fn)21.7420.623.2

for loop + push (ES5) beat Array.from({length}, fn) by 2.66x

What do you prefer?

5 Upvotes

10 comments sorted by

View all comments

1

u/WystanH Jul 22 '26

Premature optimization is the root of all evil.

If you need to chug through a million plus values, then you may need to begin to consider the impact to the approach used. You could be forced to choose efficiency over clarity if the job calls for it. In any other instance, choose clarity.

1

u/VillageRemarkable188 Jul 22 '26

I’ve heard that money is the root of all evil, so does that mean that premature optimization is the root of all money? ;)

1

u/WystanH Jul 22 '26

I suppose avarice is at the root of both. Greed for money causes suffering. Greed for speed causes crap code.