Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Creating partial objects (Array.reduce vs for loop)
(version: 0)
Comparing performance of:
Array.reduce vs for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var keys = ['foo', 'baz'] var obj = { foo: 195, bar: 'testing', baz: 'more testing', qux: true }
Tests:
Array.reduce
const partial = keys.reduce((acc, cur) => Object.assign(acc, { [cur]: obj[cur] }), {})
for loop
const partial = {} for (const key of keys) partial[key] = obj[key]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.reduce
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 break down the provided benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark compares two approaches for creating partial objects: using `Array.reduce()` versus using a traditional `for` loop. The test creates an object with some keys (`foo`, `baz`) and their corresponding values, and then attempts to create a new object that only includes these specific properties. **Approach 1: Array.reduce()** The first approach uses `Array.reduce()` to iterate over the array of keys (`keys`) and create a new object by merging the properties of the original object (`obj`). The `reduce()` method applies a callback function to each element in the array, accumulating a result. In this case, the callback function `Object.assign(acc, { [cur]: obj[cur] })` merges the current property into the accumulator (`acc`) and returns the new merged object. Pros: * Concise and expressive code * Easy to read and maintain * Optimized for performance Cons: * Limited control over the iteration process * May have performance issues if the array is very large or the callback function is complex **Approach 2: Traditional for loop** The second approach uses a traditional `for` loop to iterate over the array of keys (`keys`) and create a new object by assigning properties manually. The loop iterates over each key, creates a property with the corresponding value from the original object (`obj[key]`), and assigns it to the partial object. Pros: * More control over the iteration process * Can be more flexible for complex logic or edge cases Cons: * More verbose and less readable than `Array.reduce()` * May have performance issues if the array is very large or the loop is complex **Library/Functionality** In this benchmark, no libraries are used beyond the standard JavaScript features. However, it's worth noting that `Object.assign()` is a part of the ECMAScript specification and is widely supported across modern browsers. **Special JS Feature/Syntax** No special JavaScript features or syntax are mentioned in the benchmark definition or test cases. **Other Alternatives** If you're interested in exploring alternative approaches to creating partial objects, here are some other options: 1. `Object.create()`: Similar to `Array.reduce()`, but uses the prototype chain instead of an array. 2. `for...in` loop: Iterates over the properties of the original object using its own syntax. 3. `map()` and `reduce()` together: Can be used to create a partial object by mapping over the array of keys and reducing it into a single object. 4. Object literal with spread syntax (`{ ... }`: Creates an object from an existing object by spreading its properties onto a new object. Keep in mind that each approach has its own trade-offs, and the best choice will depend on the specific use case and requirements.
Related benchmarks:
Array.prototype.reduce() vs _.get() of lodash for nested objects without arrays
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values
Loop over object: lodash vs Object.entries vs Object.values vs Object.keys (lodash 4.17.15)
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values vs for of vs for in vs keys for of
Comments
Confirm delete:
Do you really want to delete benchmark?