Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce with spread operator and for
(version: 2)
Comparing performance of:
reduce with spread vs reduce without spread vs for loop
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = new Array(10000).fill(1) function withSpread(acc, item, index) { acc[index] = { id: index, timestamp: new Date(new Date().getTime() + (index * 1000)), isOdd: index % 2 === 0, isEven: index % 2 !== 0 } return { ...acc }; } function withOutSpread(acc, item, index) { acc[index] = { id: index, timestamp: new Date(new Date().getTime() + (index * 1000)), isOdd: index % 2 === 0, isEven: index % 2 !== 0 } return acc; }
Tests:
reduce with spread
array.reduce(withSpread, {})
reduce without spread
array.reduce(withOutSpread, {})
for loop
var objOfIds = {} for(var index = 0; index < array.length; index++) { objOfIds[index] = { id: index, timestamp: new Date(new Date().getTime() + (index * 1000)), isOdd: index % 2 === 0, isEven: index % 2 !== 0 } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce with spread
reduce without spread
for loop
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):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided JSON represents two benchmark definitions: `Reduce with spread operator and for` and its two variants: `Reduce with spread` and `Reduce without spread`. The latter has an additional variant using a traditional `for` loop. **Tested Options** 1. **Spread Operator (`...`)**: The spread operator is used to expand the object `acc` into a new array in the `withSpread` function. 2. **Traditional `for` Loop**: A classic `for` loop is used to iterate over the elements of the array in the `for` variant. **Pros and Cons** 1. **Spread Operator (withSpread)**: * Pros: More concise, readable, and maintainable code. It also avoids the need for explicit indexing. * Cons: Can lead to performance issues if not optimized correctly, as it creates a new array and assigns references to each element. 2. **Traditional `for` Loop**: * Pros: Well-established and efficient approach, especially when working with large datasets. * Cons: More verbose code, which can make it harder to read and maintain. **Library Usage** None of the provided benchmark definitions use any external libraries. **Special JavaScript Features/Syntax** The spread operator (`...`) is a relatively recent addition to JavaScript (introduced in ECMAScript 2018). It's used to expand an object into a new array, allowing for more concise and expressive code. The traditional `for` loop syntax remains unchanged. **Alternative Approaches** 1. **Array.prototype.forEach()**: This method iterates over the elements of an array without creating a new array or using indexing. 2. **Arrow Functions**: Instead of defining a separate function with the spread operator, you can use an arrow function to achieve similar results in a more concise way. These alternative approaches might offer different performance characteristics and trade-offs compared to the original benchmark definitions. **Benchmark Result Interpretation** The latest benchmark result shows three execution counts for each test case: * `Reduce without spread`: 493.9852600097656 executions per second * `For loop`: 438.6302490234375 executions per second * `Reduce with spread`: 86.71508026123047 executions per second These results indicate that the traditional `for` loop approach is generally faster than both the spread operator and the original `withSpread` function. Keep in mind that these results might vary depending on specific use cases, system configurations, and JavaScript engines.
Related benchmarks:
Spread vs mutating
Spread vs mutating
Array spread operator vs push 2
spread vs assign in loop
push vs spread (reduce array)
Comments
Confirm delete:
Do you really want to delete benchmark?