Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs immutable Object.assign performance
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
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);
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 what's being tested in this benchmark. The benchmark is comparing two approaches to merge two objects in JavaScript: 1. **Spread Operator (`...`)**: This syntax was introduced in ECMAScript 2018 (ES2018). It allows you to expand an object literal by spreading its properties onto another object, effectively creating a new object that combines the two. 2. **Immutable `Object.assign()` method**: The `Object.assign()` method is used to copy the values of all enumerable own properties from one or more source objects into a destination object. **Options Compared:** * Spread Operator (`...`) * Immutable `Object.assign()` method **Pros and Cons of Each Approach:** 1. **Spread Operator (`...`)**: * Pros: + More concise and readable syntax. + Less error-prone, as it's harder to accidentally overwrite properties. * Cons: + May be slower due to the overhead of creating a new object using `Object.create()` or `Array.prototype.slice()`. 2. **Immutable `Object.assign()` method**: * Pros: + Can be faster for large objects, as it uses a single function call with multiple arguments. * Cons: + More verbose syntax and prone to errors (e.g., incorrect property ordering). **Other Considerations:** * The benchmark doesn't consider the impact of object size or complexity on performance. As mentioned earlier, larger objects might favor `Object.assign()`. * No consideration is given to browser-specific optimizations or caching mechanisms. **Library Used:** No explicit library is used in this benchmark. However, some browsers might use internal libraries or optimized implementations of these APIs. **Special JS Feature/Syntax:** None are explicitly mentioned in the provided code snippets. **Benchmark Preparation Code and JSON:** The provided `Script Preparation Code` and `Html Preparation Code` are empty, suggesting that the user has not contributed any additional setup or test harness code. The benchmark definitions are stored in a JSON file, which defines the two test cases: 1. "Using the spread operator" 2. "Using Object.assign" **Benchmark Results:** The latest benchmark results show that Chrome 68 executes the `Object.assign()` method approximately 34% faster than the spread operator (`...`) for this specific use case. Keep in mind that these results might not be representative of all browsers or scenarios, as the benchmark's scope is limited to a single browser and test setup.
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?