Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Measure iterations1
(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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 YaBrowser/24.4.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Loop with conditions
49380.6 Ops/sec
Iterator
60712.5 Ops/sec
Generator
64019.7 Ops/sec
.forEach
49096.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript iteration methods can be a complex task, as it depends on various factors such as hardware, browser engine, and specific code optimizations. In this benchmark, we have four test cases: 1. **Loop with conditions**: This is a traditional loop that uses `if` statements to filter elements from an array. 2. **Iterator**: In this case, the same array is filtered using an iterator function (`filter(arr, predicate)`). 3. **Generator**: The same array is filtered using a generator function (`filterGenerator(arr, predicate)`). 4. **.forEach**: This test uses the `forEach` method to iterate over the array and filter elements. **Options compared:** The benchmark compares four different approaches: * Loop with conditions (traditional loop) * Iterator (using an iterator function) * Generator (using a generator function) * .forEach (using the `forEach` method) **Pros and Cons:** 1. **Loop with conditions**: This is the most straightforward approach, but it can be slow due to the overhead of the loop and the conditionals. * Pros: Easy to understand and implement * Cons: Can be slow and inefficient 2. **Iterator**: Using an iterator function provides a good balance between performance and readability. * Pros: More efficient than traditional loops, readable code * Cons: Requires creating an iterator object 3. **Generator**: Generator functions are similar to iterator functions but provide additional features like async iteration. * Pros: Fast execution, readable code, support for async iteration * Cons: Requires knowledge of generator syntax and async programming concepts 4. **.forEach**: The `forEach` method is often a convenient choice, but it can be slower than other methods due to the overhead of the function call. * Pros: Convenient, easy to understand * Cons: Can be slower than other methods **Library usage:** The test case uses the built-in `Array.prototype.filter()` method, which creates an iterator object internally. The `filterGenerator` function is a custom implementation that also creates an iterator object. **Special JS feature:** There are no special JavaScript features or syntaxes used in this benchmark. The code is straightforward and follows standard JavaScript best practices. **Other alternatives:** If you want to explore more iteration methods, consider the following: * `map()`: Creates a new array with the results of applying a provided function on every element in the original array. * `reduce()`: Applies a reducing function against an accumulator and each element in the array (from left to right) to reduce it to a single value. * `for...of` loops: Iterate over arrays or iterable objects using a more concise syntax. These alternatives may offer different trade-offs between performance, readability, and convenience, so be sure to test them with your specific use cases.
Related benchmarks:
filter-vs-reduce-1
Array.prototype.filter vs Lodash filter vs L
Native filter().length vs lodash sumBy vs lodash filter().size(), for small array
Filtering + Mapping
Filtering and mapping with .filter(...).map(...) vs .flatMap(...) vs custom filterMap(...)
Comments
Confirm delete:
Do you really want to delete benchmark?