Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test reduc
(version: 0)
Comparing performance of:
reduce vs for
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array(10000).fill({key: 'key', value : 'value'})
Tests:
reduce
data.reduce((acc, current) => { acc[current.key] = current.value; return acc; }, {});
for
const acc = {}; for(let i=0 ; i<data.length ; i++) { acc[data[i].key] = data[i].value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
for
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'd be happy to help explain the JavaScript microbenchmark on MeasureThat.net. **Overview of the Benchmark** The provided benchmark measures the performance difference between two approaches for reducing an object in JavaScript: the `Array.prototype.reduce()` method and a traditional `for` loop. **Script Preparation Code** The script preparation code defines an array of 10,000 objects with a single key-value pair each: ```javascript var data = Array(10000).fill({key: 'key', value: 'value'}); ``` This array will be used as input for both benchmarking approaches. **Benchmark Definition** There are two benchmark definitions: 1. **`data.reduce()`**: This line of code uses the `Array.prototype.reduce()` method to iterate over the `data` array and accumulate a new object with key-value pairs. ```javascript data.reduce((acc, current) => { acc[current.key] = current.value; return acc; }, {}); ``` This approach is concise and expressive, but it may incur some overhead due to the creation of an intermediate result object. 2. **`for` loop**: This code uses a traditional `for` loop to iterate over the `data` array and accumulate a new object with key-value pairs. ```javascript const acc = {}; for (let i = 0; i < data.length; i++) { acc[data[i].key] = data[i].value; } ``` This approach is more verbose, but it can be faster since it avoids the creation of an intermediate result object. **Options Compared** The two benchmarking approaches differ in their use of an intermediate result object. The `data.reduce()` method creates a new object to store the accumulated key-value pairs, while the `for` loop accumulates values directly in the `acc` object. **Pros and Cons** * **`data.reduce()`**: Pros: + Concise and expressive code + Easy to read and maintain * Cons: + May incur overhead due to intermediate result object creation * **`for` loop**: Pros: + Can be faster since it avoids intermediate result object creation + Allows for direct access to the `acc` object * Cons: + More verbose code + Requires explicit looping and indexing **Library and Special JS Feature** In this benchmark, there are no external libraries used. **Special JS Features** There is no special JavaScript feature or syntax used in these benchmarking approaches. The code is standard ECMAScript 2022 compliant. **Other Alternatives** Other alternatives for reducing an object in JavaScript could include: 1. Using `Array.prototype.forEach()` with a callback function. ```javascript data.forEach((item) => { acc[item.key] = item.value; }); ``` This approach has similar performance characteristics to the `for` loop, but is slightly more concise. 2. Using `Array.prototype.map()` and then using an object literal to accumulate values. ```javascript const mappedData = data.map((item) => ({ key: item.key, value: item.value })); const acc = {}; mappedData.forEach((item) => { acc[item.key] = item.value; }); ``` This approach is more concise than the `for` loop, but may incur additional overhead due to the use of `map()`. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the original benchmarking approaches.
Related benchmarks:
Array range generating
Test of
get value from dict of array
Array.fill vs Array.from with dyamnic data
Comments
Confirm delete:
Do you really want to delete benchmark?