Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Measure iterations
(version: 0)
Comparing performance of:
Loop with conditions vs Iterator vs Generator vs .forEach
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function filter(iterable, predicate) { const iter = iterable[Symbol.iterator](); return { [Symbol.iterator]() { return this; }, next() { let result; while ( !(result = iter.next()).done && !predicate(result.value) ); return result; } }; } function* filterGenerator(iterable, predicate){ for (const value of iterable) { if (predicate(value)) { yield value; } } } var arr = Array(100).fill(0).map((_, i) => i + 1); var predicate = (x) => x % 3 === 0 && x % 5 === 0;
Tests:
Loop with conditions
for (const value of arr) { if (predicate(value)) { console.log('foobar'); } }
Iterator
for (const value of filter(arr, predicate) { console.log('foobar'); }
Generator
for (const value of filterGenerator(arr, predicate) { console.log('foobar'); }
.forEach
arr.forEach((x) => predicate(x) && console.log('foobar'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Loop with conditions
Iterator
Generator
.forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Comments
Confirm delete:
Do you really want to delete benchmark?