Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
iterators vs callbacks
(version: 0)
Comparing performance of:
iterator vs callback
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function* iterator(size) { for (let i = 0; i < size; i++) { yield i; } } function callback(size, cb) { for (let i = 0; i < size; i++) { cb(i); } } var N = 100000;
Tests:
iterator
let sum = 0; for (const i of iterator(N)) { sum += i*Math.random(); }
callback
let sum = 0; callback(N, i => { sum += i*Math.random(); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
iterator
callback
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
iterator
73.0 Ops/sec
callback
378.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this specific benchmark. **Benchmark Definition** The benchmark is designed to compare two approaches: using iterators and using callbacks. Both methods are used to iterate over a large dataset (100,000 elements) and perform a calculation on each element. **Iterator Approach** In the iterator approach, a generator function `iterator(size)` is defined. This function uses a for loop to iterate over the range of numbers from 0 to `size-1` (since JavaScript arrays are 0-indexed). The `yield` keyword is used to produce values from the generator, which are then collected by the caller. In the test case, the iterator is used in a for-of loop to iterate over the generated values. For each value, it multiplies the value with a random number between 0 and 1 and accumulates the result in the `sum` variable. **Callback Approach** The callback approach uses a separate function `callback(size, cb)` that takes two arguments: `size` (the same as in the iterator approach) and a callback function `cb`. The callback function is expected to take one argument (`i`) and perform some operation on it. In the test case, the callback function is passed as an argument to the `callback` function. For each value in the range of numbers from 0 to `size-1`, the callback function is called with that value as an argument, multiplying it by a random number between 0 and 1 and accumulating the result in the `sum` variable. **Comparison** The two approaches are compared based on their performance. The benchmark measures the number of executions per second (ExecutionsPerSecond) for each approach. **Pros and Cons** **Iterator Approach:** Pros: * More efficient memory usage, as it doesn't create an intermediate array to store the values. * Can be more concise and expressive in some cases. Cons: * May be slower due to the overhead of the generator function. * Not all browsers support generators (although most modern ones do). **Callback Approach:** Pros: * More widely supported by older browsers, as it doesn't rely on generators. * Can be easier to understand for developers familiar with callbacks. Cons: * Creates an intermediate array to store the values, which can lead to higher memory usage. * May be less concise and expressive than the iterator approach. **Library Usage** There is no explicit library usage in this benchmark. However, it's worth noting that MeasureThat.net often uses libraries like Lodash or Ramda for more complex benchmarks. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond generators and callbacks. If you're interested in exploring other features, such as async/await, promise chaining, or modern ES6+ syntax, MeasureThat.net can definitely accommodate that! **Alternatives** If you'd like to explore similar benchmarks or compare different approaches on your own, some alternatives include: * Benchmarking libraries likeBenchmark.js or HighPerformanceBenchmarks. * Online platforms like jsbench.net or CodePen's benchmarking feature. Keep in mind that these alternatives might not be as comprehensive or well-maintained as MeasureThat.net.
Related benchmarks:
Var vs Let
Bound Symbol.iterator effect on for..of loop performance
Iterator vs Index for loop
Iterator vs Index for loop (global sum)
Comments
Confirm delete:
Do you really want to delete benchmark?