Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop Test
(version: 0)
Comparing performance of:
Imperative vs Multiple reduces vs Single reduce
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var xs = []; for( var x = 0; x < 1000000; x++ ) xs[x] = x;
Tests:
Imperative
var count = 0; var sum = 0; xs.forEach(function(x){ count = count + 1; sum = sum + x; }); console.log("Baseline: ", count, sum);
Multiple reduces
var totalSum = xs.reduce(function(x,y){ return x + y }); var totalCount = xs.reduce(function(n, _){ return n + 1 }); console.log("Multiple: ", totalSum, totalCount);
Single reduce
var initialState = { count: 0, sum: 0 } function step( state, n ){ return { count: state.count + 1, sum: state.sum + n } } var results = xs.reduce( step, initialState ); console.log("Single Reduce:", results);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Imperative
Multiple reduces
Single reduce
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!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its various components, explaining what's being tested and the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance of three different ways to sum up the values in an array `xs`. The array is initialized with 1 million elements using the script preparation code: `var xs = []; for (var x = 0; x < 1000000; x++) xs[x] = x;`. **Test Cases** There are three test cases: ### Imperative ```javascript var count = 0; var sum = 0; xs.forEach(function(x){ count = count + 1; sum = sum + x; }); console.log("Baseline: ", count, sum); ``` This test case uses a traditional loop to iterate through the array `xs` and perform the summation. The variable `count` is incremented by 1 for each element, while `sum` accumulates the values. **Pros:** Simple and easy to understand. **Cons:** May not be optimized for performance, as it uses explicit loops and arithmetic operations. ### Multiple reduces ```javascript var totalSum = xs.reduce(function(x,y){ return x + y; }, 0); var totalCount = xs.reduce(function(n, _){ return n + 1; }, 0); console.log("Multiple: ", totalSum, totalCount); ``` This test case uses the `reduce()` method to perform two separate reductions: * First, it sums up all elements in the array using `totalSum = xs.reduce(...)`. * Second, it counts the number of elements by adding 1 for each element (which is essentially an identity function) using `totalCount = xs.reduce(...)`. **Pros:** Reduces boilerplate code and makes the implementation more concise. **Cons:** The use of identity functions may not provide a meaningful result if not used correctly. ### Single reduce ```javascript var initialState = { count: 0, sum: 0 } function step(state, n){ return { count: state.count + 1, sum: state.sum + n } } var results = xs.reduce(step, initialState); console.log("Single Reduce:", results); ``` This test case uses a single reduction to perform both operations in one pass. The `step()` function takes the current state and an element from the array, updating both `count` and `sum`. The initial state is defined outside the `reduce()` method. **Pros:** Optimized for performance by reducing the number of iterations. **Cons:** Requires more complex logic (the `step()` function) to keep track of both accumulations. **Libraries/Functions Used** * `forEach()`: Iterates over an array and executes a provided callback function once for each element in the array. * `reduce()`: Reduces an array to a single value by applying a function to all elements. * Array.prototype.forEach(): A built-in method that iterates over arrays. **Special JavaScript Features/ Syntax** None are explicitly mentioned or used in this benchmark. **Alternatives** Other ways to sum up the values in `xs` could include: * Using `Map()` and `forEach()`: Convert the array to a map, iterate through it using `forEach()`, and accumulate values. * Using `Array.prototype.every()` and `reduce()`: Use `every()` with an identity function as the predicate and reduce the resulting boolean value. However, these alternatives would likely incur additional overhead due to their use of more complex methods or data structures.
Related benchmarks:
Plus equals is slow
Plus equals is slow
Plus equals is slow 222
Plus equals is slow 333
Simple value assignment
Comments
Confirm delete:
Do you really want to delete benchmark?