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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser:
Chrome 120
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
for
2525.8 Ops/sec
for...in
5711178.5 Ops/sec
for...of
2500.3 Ops/sec
forEach
1042885.1 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'))