Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance fixed 22
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var firstObject = { "id": "2d10ffad-678b-4f9b-9d7e-1fd57879928d", "name": "Gon Freecss", "age": 100, "wallet": { "CNY": { "balance": 94306.78, "exchangeRate": 4.45 }, "USD": { "balance": 106.84, "exchangeRate": 29.96 }, "NTD": { "balance": 89171098.51, "exchangeRate": 1 } } } var secondObject = { "id": "b8eb86c2-1386-4b91-a87f-a3b16f6ebca5", "name": "Killua Zoldyck", "age": 96, "wallet": { "CNY": { "balance": 59390.08, "exchangeRate": 4.45 }, "USD": { "balance": 446.76, "exchangeRate": 29.96 }, "NTD": { "balance": 49592399.77, "exchangeRate": 1 } } }
Tests:
Using the spread operator
const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const finalObject = Object.assign({}, firstObject, secondObject);
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 JSON data and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to merge two objects in JavaScript: using the spread operator (`...`) and using `Object.assign()`. **Options Compared** 1. **Spread Operator (`.=`)**: * Pros: concise, readable, and expressive way to merge objects. * Cons: may have performance implications due to its syntax sugar. 2. **`Object.assign()`**: * Pros: widely supported, efficient, and has been the standard for merging objects in JavaScript for a long time. * Cons: can be less readable than the spread operator, especially when dealing with complex object structures. **Other Considerations** The benchmark is also considering the browser, device platform (Mobile), and operating system (Android) to understand how these factors might impact performance. This is important because mobile devices often have limited resources and may require more efficient code to run smoothly. **Library/Functionality Used** In this benchmark, `Object.assign()` is being used as a built-in JavaScript function for merging objects. No external libraries are required. **Special JS Feature/Syntax** The spread operator (`...`) was introduced in ECMAScript 2018 (ES2018) and allows for a concise way to merge objects using the syntax `${object1} { ... } ${object2}` or `...${object1}, ...${object2}`. This syntax sugar makes the code more readable but might also introduce performance overhead. **Benchmark Preparation Code** The preparation code creates two object literals, `firstObject` and `secondObject`, which are then used to create the benchmarked objects. The `Script Preparation Code` section includes the actual code that will be executed during the benchmark. **Latest Benchmark Result** The latest result shows the execution frequency per second (ExecutionsPerSecond) for each test case. In this case, the spread operator performs slightly better than `Object.assign()`. If you're interested in optimizing JavaScript performance or comparing different approaches to merging objects, this benchmark can be a helpful reference point. **Other Alternatives** For merging objects, other alternatives exist: 1. **Lodash's `merge()` function**: A popular utility library that provides a robust and efficient way to merge objects. 2. **Immutable.js**: A library that encourages functional programming and provides a more predictable, thread-safe way to work with immutable data structures. Keep in mind that these alternatives might have different performance characteristics or trade-offs depending on the specific use case.
Related benchmarks:
JavaScript spread operator vs Object.assign performance (larger)
JavaScript spread operator vs Object.assign performance create new
JavaScript spread operator vs Object.assign vs for-in loop performance
JavaScript spread operator vs Object.assign vs for-in loop safe performance
JavaScript spread operator vs Object.assign vs null-checked for-in loop performance
Comments
Confirm delete:
Do you really want to delete benchmark?