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

Show parent comment

1

u/breacket Jul 22 '26

Was posted here on /learnjavascript because moderators have declined for /javascript. Is not a post just for learners. I've seen that "syntax" problems in apps/platforms in production.

The benchmarks you see are real, not fictional. That scores are on current hardware, not from 10 years ago.

Yes, for the most use cases I agree with you, does not matter so much. But, sometimes are vital – is about that situations.

The hardware does not somehow using magic to optimize the app because you use the slow syntax. Will be just slower in final.