Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance vs Multiple statementd
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Multiple statements
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
let firstObject = { sampleData: 'Hello world' } const secondObject = { sampleData: 'Overwritten', moreData: 'foo bar' } firstObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { sampleData: 'Overwritten', moreData: 'foo bar' } Object.assign(firstObject, secondObject);
Multiple statements
const firstObject = { sampleData: 'Hello world' } const secondObject = { sampleData: 'Overwritten', moreData: 'foo bar' } firstObject.sampleData = secondObject.sampleData; firstObject.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
Multiple statements
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.1:latest
, generated one year ago):
Let's break down this benchmark test case by case. **Benchmark Definition:** The benchmark compares three different ways to update an object in JavaScript: 1. Using the spread operator (`...`) 2. Using `Object.assign()` 3. Updating properties individually using multiple statements **Individual Test Cases:** ### 1. **Using the spread operator** ```javascript let firstObject = { sampleData: 'Hello world' } const secondObject = { sampleData: 'Overwritten', moreData: 'foo bar' } firstObject = { ...firstObject, ...secondObject }; ``` In this test case, we're using the spread operator (`...`) to merge `secondObject` into `firstObject`. The spread operator is a feature of JavaScript that allows us to expand an array or object into a new one. In this case, it's used to update the properties of `firstObject` with those from `secondObject`. **Library/Feature:** None **Pros/Cons:** * Pros: + Concise and expressive syntax + Easy to read and write * Cons: + Can be slower than other methods for large objects (as shown in the benchmark results) ### 2. **Using Object.assign()** ```javascript const firstObject = { sampleData: 'Hello world' } const secondObject = { sampleData: 'Overwritten', moreData: 'foo bar' } Object.assign(firstObject, secondObject); ``` In this test case, we're using the `Object.assign()` method to update `firstObject` with properties from `secondObject`. This method creates a new object by copying the values of one or more objects. **Library/Feature:** `Object.assign()`, which is a standard JavaScript method **Pros/Cons:** * Pros: + Fast and efficient, especially for large objects + Easy to use and understand * Cons: + Can be slower than other methods when used with small objects (as shown in the benchmark results) ### 3. **Multiple statements** ```javascript const firstObject = { sampleData: 'Hello world' } const secondObject = { sampleData: 'Overwritten', moreData: 'foo bar' } firstObject.sampleData = secondObject.sampleData; firstObject.moreData = secondObject.moreData; ``` In this test case, we're updating `firstObject` by setting each property individually using assignment (`=`). This method is simple and easy to understand but can be slower than other methods. **Library/Feature:** None **Pros/Cons:** * Pros: + Simple and easy to read + Can be faster than spread operator for small objects (as shown in the benchmark results) * Cons: + More verbose than other methods, which can make it harder to read and write **Benchmark Results:** The latest benchmark results show that: * Multiple statements are the fastest method (34334988.0 executions per second) * Object.assign() is slower than multiple statements but faster than the spread operator * The spread operator is the slowest method among the three **Alternatives:** If you need to update an object with properties from another, consider using `Object.assign()` or a library like Lodash's `assign()` function. If performance is crucial and your objects are small, multiple statements might be a good choice. Keep in mind that these results may vary depending on the specific JavaScript engine, browser, or environment you're using.
Related benchmarks:
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance 2 - kevin
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?