Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Js clone object - spread operator vs Object.assign performance
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world', recursive: { data1: "nothing" } } const secondObject = { moreData: 'foo bar', recursive: { data2: "something" } } const finalObject = { ...firstObject, ...secondObject, recursive: { ...firstObject.recursive, ...secondObject.recursive } };
Using Object.assign
const firstObject = { sampleData: 'Hello world', recursive: { data1: "nothing" } } const secondObject = { moreData: 'foo bar', recursive: { data2: "something" } } const finalObject = Object.assign({}, firstObject, secondObject, { recursive: Object.assign({}, firstObject.recursive, secondObject.recursive)});
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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for creating a new object by merging two existing objects: using the spread operator (`...`) and `Object.assign()`. The test case creates two sample objects, `firstObject` and `secondObject`, with some common properties and nested objects. Then, it attempts to merge these objects into a single object, `finalObject`. **Test Case 1: Using the Spread Operator** In this test case, the spread operator (`...`) is used to create a new object that includes all the properties of both `firstObject` and `secondObject`. The code: ```javascript const finalObject = { ...firstObject, ...secondObject, recursive: { ...firstObject.recursive, ...secondObject.recursive } }; ``` The spread operator is a concise way to create a new object with all the key-value pairs from one or more source objects. **Pros of Using the Spread Operator:** * Concise and readable code * Easy to maintain and modify **Cons of Using the Spread Operator:** * Performance overhead due to recursive function calls * May not be optimized for large objects or deep nested structures **Test Case 2: Using Object.assign()** In this test case, `Object.assign()` is used to create a new object by merging two existing objects. The code: ```javascript const finalObject = Object.assign({}, firstObject, secondObject, { recursive: Object.assign({}, firstObject.recursive, secondObject.recursive) }); ``` `Object.assign()` is a method that copies the properties from one or more source objects to a target object. **Pros of Using Object.assign():** * Performance-optimized for large objects and deep nested structures * Can be used with older browsers that don't support spread operator **Cons of Using Object.assign():** * More verbose code compared to the spread operator * May require additional function calls for nested objects **Library Used:** In both test cases, no specific library is used. The benchmark is testing built-in JavaScript features. **Special JS Features/Syntax:** The spread operator (`...`) and `Object.assign()` are two relatively recent features in JavaScript that provide concise ways to merge objects. However, since the benchmark uses older browsers (specifically Chrome 108), it's likely that these features were introduced later in the browser's development cycle. **Alternative Approaches:** Other alternatives for merging objects include: * Using a library like Lodash (`_.merge()`) * Using a custom implementation with recursive function calls * Using `JSON.parse()` and `JSON.stringify()` to serialize and deserialize the objects These alternative approaches may have different performance characteristics and trade-offs compared to using the spread operator or `Object.assign()`.
Related benchmarks:
JS object copy spread vs assign
JavaScript spread operator vs Object.assign performance for cloning
Object.assign mutation vs spread
object.assign vs spread operator for shallow copying large objects 2
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Comments
Confirm delete:
Do you really want to delete benchmark?