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

0

u/TheRNGuy Jul 22 '26

No, ES6 is same or faster, and more important, better syntax.

In what program do you ever need to sum million times?

1

u/breacket Jul 22 '26

Based on some benchmarks and real world use cases, some ES6 syntax is slower than ES5 (I don't mean about the "engine" of ES6, but about some practices like using "for ...of" instead of the classic "for").

Where?

  • processing data from databases
  • large datasets or even small datasets but with many links
  • apps launched on modern devices that you need to also work smooth on old devices

That "sum million times" is not a real use case in general but is one valid for benchmarking that reflects some real use cases in production. Somehow abstract.