Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object from entries - Object.assign vs. spread
(version: 0)
Tests the difference in performance when reconstructing an object from its entries via reduce, between using Object.assign and the object rest spread operator
Comparing performance of:
Object.assign vs Spread
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.entries = [ ['one', 1], ['two', 2], ['three', 3], ['four', 4], ['five', 5] ];
Tests:
Object.assign
window.entries.reduce((obj, [key, value]) => Object.assign(obj, {[key]: value}), {})
Spread
window.entries.reduce((obj, [key, value]) => ({...obj, [key]: value}), {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
Spread
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 explain what's being tested. **Benchmark Definition** The benchmark is testing the performance difference between two approaches to reconstruct an object from its entries via reduce: 1. Using `Object.assign` 2. The object rest spread operator (a new feature introduced in ECMAScript 2018, also known as "rest property") Both approaches aim to create a new object by reducing an array of key-value pairs and assigning the values to the object. **Options being compared** The two options being compared are: 1. `Object.assign(obj, {[key]: value})` - This approach uses the spread operator to create a new object with the key-value pairs from the array. 2. `{...obj, [key]: value}` - This approach uses the rest property (introduced in ECMAScript 2018) to create a new object by spreading the existing `obj` and adding the new key-value pair. **Pros and Cons** 1. **Object.assign**: Pros: * Widely supported across browsers and Node.js versions. * Easy to understand and use, especially for those familiar with the original `assign()` method. Cons: * May be slower due to the need to iterate over the object's properties and assign them individually. 2. **Rest property (Spread)**: Pros: * More concise and expressive than `Object.assign`. * Can lead to better code readability and maintainability. Cons: * Less widely supported across older browsers and Node.js versions (although this is improving). * May require a basic understanding of the spread operator. **Library usage** There doesn't appear to be any explicit library usage in these benchmark definitions. However, it's worth noting that `Object.assign()` is part of the standard JavaScript object prototype, while the rest property (Spread) was introduced as a new feature in ECMAScript 2018. **Special JS features or syntax** This benchmark uses a special feature of modern JavaScript: the rest property (introduced in ECMAScript 2018). The spread operator is also used, which was introduced in ECMAScript 5.1 (2012). **Other alternatives** If you're interested in exploring other approaches to reconstructing an object from its entries via reduce, some alternative methods might include: * Using a library like Lodash's `reduce` method with a custom object creator function. * Implementing a custom reducer function using a different data structure, such as a Map or an array of objects. However, for this specific benchmark, the two options being compared (Object.assign and spread) are likely to be the most relevant, as they aim to address common use cases and performance considerations in JavaScript.
Related benchmarks:
JavaScript spread operator vs Object.assign performance without mutating original object
JavaScript spread operator vs Object.assign performance with empty object as first key
JavaScript spread operator vs Object.assign performance without overwriting original object
JavaScript spread operator vs Object.assign performance with new object in assign
JavaScript spread operator vs Object.assign performance v3455676854743
Comments
Confirm delete:
Do you really want to delete benchmark?