Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign 3
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var firstObject = Array.from({ length: 100 }).reduce((acc, _el, index) => { acc[`some_${index}`] = index; return acc; }, {}); var loading = true;
Tests:
Using the spread operator
const finalObject = { ...firstObject, loading, };
Using Object.assign
const finalObject = Object.assign({ loading }, firstObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the spread operator
Using 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 JSON and explain what is tested on the benchmark. The benchmark tests two approaches for creating a new object by copying an existing object with additional properties: using the JavaScript spread operator (`...`) and `Object.assign()`. The test case creates a large object `firstObject` with 100 properties, each containing an index value. Then, it defines two benchmarks: 1. **Using the spread operator**: ```javascript const finalObject = { ...firstObject, loading: true }; ``` In this approach, we use the spread operator (`...`) to create a new object `finalObject` by copying all properties from `firstObject`. We then add an additional property `loading` with value `true`. Pros: * Concise and expressive syntax * Works with objects and arrays Cons: * Performance might be slower due to function call overhead (as of ES6) * May lead to unexpected behavior if used incorrectly (e.g., spreading non-object values) 2. **Using Object.assign()**: ```javascript const finalObject = Object.assign({ loading: true }, firstObject); ``` In this approach, we use the `Object.assign()` method to merge two objects: an empty object with a single property `loading` and the original object `firstObject`. The resulting object is assigned to `finalObject`. Pros: * Well-established API * Works with any type of object (not just primitive values) Cons: * Requires more code than the spread operator approach * May be slower due to method call overhead In both cases, we're creating a new object by copying an existing object and adding one or more properties. The difference lies in the syntax and performance implications. **Library usage:** There is no library explicitly mentioned in the provided JSON. However, it's worth noting that `Object.assign()` is a built-in method in JavaScript since ES5. **Special JS feature or syntax:** The spread operator (`...`) was introduced in ES6 and allows object spreading, which enables creating new objects by copying existing properties. It's a concise and expressive way to create new objects but can lead to unexpected behavior if not used carefully. **Alternative approaches:** Other alternatives for creating new objects with additional properties include: * Using the `lodash` library's `cloneDeep()` method * Using the `immer` library's `produce()` function * Manual property copying using a loop or recursion Keep in mind that these alternative approaches may have different performance characteristics, syntax complexities, and use cases compared to the spread operator and `Object.assign()` methods.
Related benchmarks:
JavaScript spread operator vs Object.assign
JavaScript spread operator vs Object.assign 2
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?