Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
How to reduce an object to a sub-object fast
(version: 0)
Comparing performance of:
selectReduceAssign vs selectReduceSpread vs selectReduceAddExplicitly vs selectPick
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var source = { a: 0, b: 1, c: 2, d: 3, e: 4, f: 5, various: null } var select = ['a', 'b', 'c', 'd', 'e', 'f'] var selectReduceAssign = () => select.reduce( (accumulator, current) => Object.assign(accumulator, { [current]: source[current] }), {} ) var selectReduceSpread = () => select.reduce( (accumulator, current) => ({ ...accumulator, [current]: source[current] }), {} ) var selectReduceAddExplicitly = () => select.reduce( (accumulator, current) => { accumulator[current] = source[current] return accumulator }, {} ) var selectPick = ({ a, b, c, d, e, f }) => ({ a, b, c, d, e, f })
Tests:
selectReduceAssign
selectReduceAssign()
selectReduceSpread
selectReduceSpread()
selectReduceAddExplicitly
selectReduceAddExplicitly()
selectPick
selectPick(source)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
selectReduceAssign
selectReduceSpread
selectReduceAddExplicitly
selectPick
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 definition and test cases to understand what is being tested. **Benchmark Definition:** The benchmark measures how fast it can reduce an object (in this case, `source`) to a sub-object by selecting specific properties (`a`, `b`, `c`, `d`, `e`, and `f`) and assigning their values to the resulting sub-object. The approach used is a reduction function applied to an array of selected indices. **Test Cases:** 1. `selectReduceAssign()`: This test case uses the `reduce()` method with an initial accumulator value of `{}` and assigns each property from `source` to the accumulator object using `Object.assign()`. The callback function returns the updated accumulator. 2. `selectReduceSpread()`: Similar to the previous test, but it uses the spread operator (`{ ...accumulator, [current]: source[current] }`) instead of `Object.assign()` to assign properties to the accumulator object. 3. `selectReduceAddExplicitly()`: This test case uses a more explicit approach by assigning each property value directly to the accumulator object using `{ accumulator[current] = source[current]; return accumulator; }`. 4. `selectPick(source)`: This test case is different from the others, as it does not use any reduction function or loop. Instead, it simply returns an object with the selected properties (`a`, `b`, `c`, `d`, `e`, and `f`) using a simple object literal. **Library/Techniques:** * `Object.assign()`: A method that assigns enumerable own properties from one or more specified values to an object. * Spread operator (`{ ...accumulator, [current]: source[current] }`): A syntax feature introduced in ECMAScript 2015 (ES6) that allows creating a new object by spreading the properties of an existing object. **Special JS Features/Syntax:** * The use of spread operator is a notable example of ES6 syntax being used in this benchmark. * `Object.assign()` is a standard JavaScript method, but its usage might be less common due to its simplicity and the existence of more modern alternatives like the spread operator. **Pros and Cons of each approach:** 1. `selectReduceAssign()`: * Pros: Concise and easy to understand. * Cons: May have performance issues if `Object.assign()` is called frequently, as it creates a new object each time. 2. `selectReduceSpread()`: * Pros: More readable and efficient than `Object.assign()`, as it avoids creating intermediate objects. * Cons: Requires ES6 support for the spread operator. 3. `selectReduceAddExplicitly()`: * Pros: Explicit and easy to understand, with minimal performance overhead. * Cons: May be less concise and more verbose than other approaches. **Other Alternatives:** 1. Using `reduce()` with a custom callback function that directly assigns properties to the accumulator object without using `Object.assign()` or spread operator. 2. Utilizing libraries like Lodash or other utility functions for array manipulation and object creation. 3. Employing more modern JavaScript features, such as `Object.fromEntries()` or `Object.assign` alternatives like `Array.prototype.forEach()` with an index mapping. In conclusion, the choice of approach depends on personal preference, performance considerations, and the desired level of conciseness in code.
Related benchmarks:
Object.fromEntries vs reduce 2.1
Object.fromEntries vs reduce2
Object.fromEntries vs reduce v21
Flatmap vs reduce with objects
Object.fromEntries vs reduce (clean)
Comments
Confirm delete:
Do you really want to delete benchmark?