Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance 1337
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
one year 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 = { sampleData: firstObject.sampleData, moreData: secondObject.moreData };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-J810G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/16.0 Chrome/92.0.4515.166 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 92 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
89050320.0 Ops/sec
Using Object.assign
382020.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test, specifically designed to compare the performance of two approaches: using the spread operator and using `Object.assign()`. **Test Case 1: Using the Spread Operator** * Benchmark Definition: The benchmark defines three objects: `firstObject`, `secondObject`, and `finalObject`. The initial object `finalObject` is empty, while `firstObject` and `secondObject` contain specific data. * Test Name: "Using the spread operator" * Script Preparation Code: None provided * Html Preparation Code: None provided **Test Case 2: Using Object.assign** * Benchmark Definition: Similar to Test Case 1, but instead of using the spread operator, it uses `Object.assign()` to merge `firstObject` and `secondObject` into a single object. * Test Name: "Using Object.assign" * Script Preparation Code: None provided * Html Preparation Code: None provided **Comparison** Both approaches aim to create a new object with merged data from `firstObject` and `secondObject`. The main difference lies in how this merging is achieved: 1. **Spread Operator (Test Case 1)**: * Pros: + Efficient, as it uses the new JavaScript feature (introduced in ES6) to spread the properties of objects. + Readable and concise code. * Cons: + May not be supported by older browsers or environments that don't implement this feature. 2. **Object.assign (Test Case 2)**: * Pros: + Widely supported across different browsers and environments, as it's a built-in method in modern JavaScript. + Less reliance on ES6 features. * Cons: + May result in slightly less readable code compared to the spread operator. **Library: Object.assign** The `Object.assign()` method is a built-in JavaScript method used for merging objects. Its primary purpose is to copy properties from source objects into a target object. In this benchmark, it's used to merge the data of two objects (`firstObject` and `secondObject`) into a single object (`finalObject`). **Special JS Feature: ES6 Spread Operator** The spread operator (introduced in ES6) allows you to use square brackets `[]` to expand an object's properties. In this benchmark, it's used to spread the properties of `firstObject` and `secondObject` into a new object (`finalObject`). This feature is not specific to modern browsers or environments. **Other Alternatives** If you want to implement this merging logic without using the spread operator or `Object.assign()`, you could use other approaches, such as: 1. Using nested loops to iterate over properties and assign values. 2. Utilizing the `for...in` loop with objects to merge data. 3. Implementing a custom function for object merging. However, these alternatives might result in less readable or more verbose code compared to using the spread operator or `Object.assign()`. **Benchmark Result Interpretation** The provided benchmark results show that the test was executed on Chrome Mobile 92 and Android device with an Execution Rate of approximately 89050320.0 executions per second (Test Case: Using the spread operator) and 382020.46875 executions per second (Test Case: Using Object.assign). The exact results might be influenced by various factors, such as system resources, browser versions, and hardware capabilities. In conclusion, the benchmark aims to evaluate the performance differences between using the spread operator and `Object.assign()` for merging objects in JavaScript.
Related benchmarks:
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)
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?