Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs assigning with a forEach
(version: 0)
Comparing performance of:
Creating an object with .reduce() vs Assigning object properties with .forEach()
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Creating an object with .reduce()
const oldObject = { one: 1, two: 2, three: 3, four: 4, five: 5 } const newObject = Object.keys(oldObject).reduce( (acc, key) => { if (key.includes('t')) return { ...acc, [key]: true } return acc }, {}, )
Assigning object properties with .forEach()
const newObject = {} const oldObject = { one: 1, two: 2, three: 3, four: 4, five: 5 } Object.keys(oldObject).forEach(key => { if (key.includes('t')) newObject[key] = true })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Creating an object with .reduce()
Assigning object properties with .forEach()
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):
The provided JSON represents a benchmark test case for JavaScript microbenchmarks on MeasureThat.net. The test compares two approaches to assign values to object properties: using the `forEach` method and using the `reduce` method. **Options Compared** 1. **Creating an object with `.reduce()`**: This approach uses the `Object.keys()` function to get an array of property names in the `oldObject`, and then applies a callback function using `.reduce()` to create a new object. The callback function checks if each key includes the substring 't' and returns an object with the matching keys. 2. **Assigning object properties with `.forEach()`**: This approach uses the `Object.keys()` function to get an array of property names in the `oldObject`, and then iterates over the array using a `forEach` loop, assigning values to the corresponding keys in the new object. **Pros and Cons** 1. **`.reduce()` method**: * Pros: More concise and expressive code, avoids explicit looping. * Cons: May be slower due to the overhead of function calls and array manipulation. 2. **`.forEach()` method**: * Pros: Faster execution time, as it avoids function call overhead. * Cons: Requires explicit looping and indexing, which can make the code less readable. In general, the `.reduce()` method is more concise and expressive, but may incur performance penalties due to its complexity. The `.forEach()` method is faster but requires more boilerplate code. **Library Used** None **Special JavaScript Features or Syntax** * None mentioned in the benchmark definition **Alternative Approaches** 1. **Using a loop**: Instead of using `reduce` or `forEach`, you could use a traditional for loop to iterate over the properties and assign values. ```javascript for (const key in oldObject) { if (key.includes('t')) newObject[key] = true; } ``` 2. **Using Object.fromEntries()**: This method creates an object from an array of key-value pairs, which can simplify the assignment process. ```javascript const entries = Object.entries(oldObject).filter(([key]) => key.includes('t')); newObject = Object.fromEntries(entries); ``` These alternative approaches may offer different trade-offs in terms of readability, performance, and conciseness.
Related benchmarks:
forEach vs reduce
forEach vs reduce
forEach vs reduce
Benchmark: flatMap vs reduce vs while vs foreach
forEach vs reduce to make Object
Comments
Confirm delete:
Do you really want to delete benchmark?