Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reduce assign vs spread
(version: 0)
Comparing performance of:
Object Assign vs Object Spread vs Object Assign Library
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Object Assign
const data = [{id: 1, category: "frontend", title: "All About That Sass"}, {id: 2, category: "backend", title: "Beam me up, Scotty: Apache Beam tips"}, {id: 3, category: "frontend", title: "Sanitizing HTML: Going antibactirial on XSS attacks"}, {id: 4, category: "frontend", title: "Doing something Odd"}, {id: 5, category: "frontend", title: "How to rebuild the moon?"}]; const reduced = data.reduce((acc, item) => { acc[item.id] = item; return acc; }, {});
Object Spread
const data = [{id: 1, category: "frontend", title: "All About That Sass"}, {id: 2, category: "backend", title: "Beam me up, Scotty: Apache Beam tips"}, {id: 3, category: "frontend", title: "Sanitizing HTML: Going antibactirial on XSS attacks"}, {id: 4, category: "frontend", title: "Doing something Odd"}, {id: 5, category: "frontend", title: "How to rebuild the moon?"}]; const reduced = data.reduce((acc, item) => ({ ...acc, [item.id]: item }), {});
Object Assign Library
const data = [{id: 1, category: "frontend", title: "All About That Sass"}, {id: 2, category: "backend", title: "Beam me up, Scotty: Apache Beam tips"}, {id: 3, category: "frontend", title: "Sanitizing HTML: Going antibactirial on XSS attacks"}, {id: 4, category: "frontend", title: "Doing something Odd"}, {id: 5, category: "frontend", title: "How to rebuild the moon?"}]; const reduced = data.reduce((acc, item) => Object.assign(acc, {[item.id]: item}), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object Assign
Object Spread
Object Assign Library
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 benchmark and its components. **Benchmark Overview** The benchmark compares three approaches to reduce an array of objects into a new object with IDs as keys: 1. **Array.prototype.reduce() with Object assignment**: This method iterates over the array, assigning each object to the accumulator using the `acc[item.id] = item` syntax. 2. **Spread operator (Object spread)**: This method uses the spread operator (`{ ...acc, [item.id]: item }`) to create a new object with the accumulator's properties spread and then add the current item as a new property. 3. **Lodash library's Object.assign()**: This benchmark uses the `Object.assign()` function from Lodash to merge the accumulator object with the current item. **Options Compared** The three options are compared in terms of execution speed, which is measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons** Here's a brief summary of each approach: 1. **Array.prototype.reduce() with Object assignment**: * Pros: Fast, efficient, and widely supported. * Cons: May not be immediately intuitive for developers without prior experience with this method. 2. **Spread operator (Object spread)**: * Pros: Concise, readable, and easy to understand. * Cons: May be slightly slower due to the overhead of creating a new object and spreading its properties. 3. **Lodash library's Object.assign()**: * Pros: Well-documented, widely supported, and can handle complex merge scenarios. * Cons: Requires an external library, which may not be desirable for all projects. **Other Considerations** When choosing between these approaches, consider the following: * If you need to perform complex merges or have a large dataset, Lodash's `Object.assign()` might be a good choice due to its flexibility and support. * For simple cases where readability is crucial, the spread operator (Object spread) might be the best option. * In general, if you're familiar with the `Array.prototype.reduce()` method, it's likely the fastest and most efficient option. **Library** In this benchmark, Lodash is used as a library for its `Object.assign()` function. Lodash provides a utility belt of functions that can simplify common tasks in JavaScript development. I hope this explanation helps!
Related benchmarks:
ES6 spread operator vs. Array.prototype.reduce()
Spread vs mutating
JavaScript spread operator vs Object.assign
JavaScript spread operator vs Object.assign 3
Object set vs new spread when reducing over results
Comments
Confirm delete:
Do you really want to delete benchmark?