Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance vs Manual assignment
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Manual assignment
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.assign(firstObject, secondObject);
Manual assignment
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = {}; finalObject.sampleData = firstObject.sampleData; secondObject.moreData = secondObject.moreData;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Manual assignment
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 what's being tested in this JavaScript benchmark. **What is being tested?** The benchmark compares the performance of three different approaches to create a new object by merging two existing objects: 1. **Using the spread operator (`...`)**: This syntax uses the spread operator to create a new object with properties from both `firstObject` and `secondObject`. 2. **Using `Object.assign()`**: This function takes multiple source objects as arguments and returns a new object created by merging all the source objects. 3. **Manual assignment**: This approach involves manually assigning properties of `firstObject` to the new object, and then assigning properties of `secondObject` to the same object. **Options compared** The benchmark is comparing the performance of these three approaches on a desktop Windows system running Chrome 112. **Pros and Cons of each approach:** 1. **Using the spread operator (`...`)**: * Pros: concise and readable syntax, efficient creation of new objects. * Cons: may not be supported in older browsers or environments that don't understand the spread operator. 2. **Using `Object.assign()`**: * Pros: widely supported across browsers and environments, easy to use and understand. * Cons: can create a new object with duplicate properties, which might lead to unexpected behavior if not used carefully. 3. **Manual assignment**: * Pros: explicit control over the creation of the new object, no dependencies on other libraries or syntax. * Cons: more verbose and less readable than the first two approaches. **Library usage** None of the test cases use any external libraries beyond what's built into JavaScript (e.g., `Object`). **Special JS feature or syntax** The spread operator (`...`) is a relatively recent addition to JavaScript, introduced in ECMAScript 2018. It allows you to create new objects by spreading the properties of an existing object. **Other considerations** To make this benchmark more comprehensive, it might be worth considering additional approaches, such as: * Using `Object.create()` or `Object.assign()` with a target object. * Using a library like Lodash's `merge()` function. * Testing performance on different types of devices (e.g., mobile, tablet). These additions would help to provide a more complete picture of the trade-offs between these approaches and ensure that the results are relevant to a broader range of use cases.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance (single addition)
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?