Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs Object.assign for mapping arrays into objects
(version: 0)
Performance of spread vs Object.assign for mapping arrays into objects
Comparing performance of:
Spread vs Object assign
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
const data = [ ["test1", 1], ["test2", 2], ["test3", 3], ["test4", 4], ["test5", 5], ["test6", 6], ["test7", 7], ["test8", 8], ["test9", 9], ] const res = data.reduce((acc, item) => ({...acc, [item[0]]: item[1]}), {})
Object assign
const data = [ ["test1", 1], ["test2", 2], ["test3", 3], ["test4", 4], ["test5", 5], ["test6", 6], ["test7", 7], ["test8", 8], ["test9", 9], ] const res = data.reduce((acc, item) => {Object.assign(acc, {[item[0]]: item[1]}); return acc;}, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
Object assign
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 dive into the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark measures the performance of two approaches for mapping arrays into objects: 1. Using the spread operator (`...`). 2. Using `Object.assign()`. Both methods are used to create a new object by iterating over an array of key-value pairs, where each pair is extracted from the array and added as a property to the new object. **Options Compared:** * **Spread Operator (`...`)**: This method uses the spread operator to create a shallow copy of the array values, which are then used to initialize properties on the new object. * **Object.assign()**: This method uses the `Object.assign()` method to merge multiple source objects into a target object. In this case, it's used to add each key-value pair from the array to the new object. **Pros and Cons:** * **Spread Operator (`...`)**: + Pros: More concise and expressive syntax, easier to read and maintain. + Cons: May have performance implications due to the creation of a shallow copy of the array values. * **Object.assign()**: + Pros: Can be more efficient for large datasets since it only copies the actual values from the sources. + Cons: May require more code and is less expressive than the spread operator. In general, if readability and conciseness are prioritized, the spread operator might be a better choice. However, if performance is a concern, using `Object.assign()` could be beneficial. **Library and Purpose:** None of the benchmarked methods rely on any specific libraries. They only use built-in JavaScript features. **Special JS Feature or Syntax:** Both methods utilize modern JavaScript syntax: * Spread operator (`...`): introduced in ECMAScript 2015 (ES6) * `Object.assign()`: also introduced in ECMAScript 2015 (ES6) These features are widely supported by modern browsers, including Firefox 68. **Alternative Approaches:** Other approaches to mapping arrays into objects could include: 1. Using the `Array.prototype.reduce()` method with a custom callback function. 2. Utilizing a library like Lodash's `mapObject()` or `merge()` functions. 3. Implementing a simple loop-based approach using an array index and an object property. These alternatives might have different performance characteristics, readability, or complexity compared to the spread operator and `Object.assign()` methods used in the benchmark.
Related benchmarks:
JavaScript spread operator vs Object.assign performance for cloning
Spread vs Object.assign (modify ) vs Object.assign (new)
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?