Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object spread vs Object.assign
(version: 0)
Comparing performance of:
object spread vs Object.assign
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = {a: 1}
Tests:
object spread
var b = {...a, b: 2}
Object.assign
var c = Object.assign({}, a, {b: 2})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object spread
Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object spread
42775248.0 Ops/sec
Object.assign
12861979.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases to understand what's being tested. **Benchmark Overview** The benchmark compares two approaches for creating objects in JavaScript: 1. **Object Spread (`...`)**: This syntax was introduced in ECMAScript 2015 (ES6) as part of the object literal notation. 2. **Object.assign()**: This method is used to create a new object by copying properties from an existing object. **Test Cases** There are two test cases: 1. **"object spread"`**: The benchmark code uses the object spread syntax to create a new object `b` that inherits the properties of `a`, and then adds a new property `b: 2`. ```javascript var b = {...a, b: 2} ``` 2. **"Object.assign()"** : The benchmark code uses the `Object.assign()` method to create a new object `c` by copying properties from `a` and adding a new property `b: 2`. ```javascript var c = Object.assign({}, a, {b: 2}) ``` **Comparison** The benchmark is testing the performance difference between these two approaches. **Pros and Cons of Each Approach** **Object Spread (`...`)** Pros: * More concise and readable syntax. * No need to create an empty object or use `Object.create()`. Cons: * Can be slower due to the overhead of creating a new object using the spread operator. * May not work as expected if the source object has non-enumerable properties. **Object.assign()** Pros: * Faster execution, especially for large objects. * Works with any type of object, including arrays and functions. Cons: * Requires more code to achieve the same result. * Can be less readable due to the method call syntax. **Library and Purpose** In this benchmark, there is no library being used. The `Object.assign()` method is a built-in JavaScript function that is part of the ECMAScript standard. **Special JS Feature or Syntax** There are no special features or syntaxes being tested in this benchmark. **Other Alternatives** Alternative approaches to object creation include: * Using the `Object.create()` method to create an object with a given prototype. * Using the `{}` literal notation and manually setting properties on the resulting object. * Using libraries like Lodash's `cloneDeep` function to clone objects. However, these alternatives are not being tested in this benchmark.
Related benchmarks:
object.assign vs spread to create a copy
Object.assign vs spreading object copy
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?