Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
for vs for...in vs for...of vs forEach
Benchmarks the speed of different iteration implementations
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser:
Firefox 121
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
for
1942.3 Ops/sec
for...in
63142388.0 Ops/sec
for...of
1902.3 Ops/sec
forEach
15152928.0 Ops/sec
Script Preparation code:
function randNum(max, min) { const x = Math.floor(Math.random() * max) if (!isFinite(min) && x < min) { return min } return x } function randString(max, min, base) { let len = randNum(max) if (min && len < min) {len = min} if (len > max) {len = max} let arr = new Uint8Array(len / 2) window.crypto.getRandomValues(arr) return Array.from(arr, (dec)=> ( dec.toString(base || 36).padStart(2, '0') )).join('') } var list = Object.freeze( Array(100).map(()=> Object.freeze({ id: randString(64,64), firstName: randString(40,5), lastName: randString(40,5), email: randString(40,5), address: Object.freeze({ country: randString(40,5), state: randString(40,5), }), })) )
Tests:
for
for (let i = 0; i < list.length; i++) {const el = list[i]; console.count('for')}
for...in
for (const elIdx in list) {const el = list[elIdx]; console.count('for...in')}
for...of
for (const el of list) {console.count('for...of')}
forEach
list.forEach((el)=> console.count('forEach'))