Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Measure iterations2
(version: 0)
Comparing performance of:
Loop with conditions vs Iterator vs Generator vs .forEach vs fori
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; var test1 = 0, test2 = 0, test3 = 0, test4 = 0, test5 = 0;
Tests:
Loop with conditions
for (const value of arr) { if (predicate(value)) { test1 += value; } } console.log(test1, 'test1');
Iterator
for (const value of filter(arr, predicate)) { test2 += value; } console.log(test2, 'test2');
Generator
for (const value of filterGenerator(arr, predicate)) { test3 += value; } console.log(test3, 'test3');
.forEach
arr.forEach((x) => predicate(x) && test4 += x); console.log(test4, 'test4');
fori
for (let i = 0; i < arr.length; i++) { if (predicate(arr[i])) { test5 += arr[i]; } } console.log(test5, 'test5');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Loop with conditions
Iterator
Generator
.forEach
fori
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?