Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Difference of Object.assign and spread operator to get a new reference
(version: 6)
Comparing performance of:
Using Object.assign vs Using spread operator vs Using a plain initialization
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
myObj = {a: 1, b: 2, c: 3}
Tests:
Using Object.assign
const newObjectA = Object.assign({}, myObj)
Using spread operator
const newObjectB = {...myObj}
Using a plain initialization
const newObjectC = JSON.parse(JSON.stringify(myObj))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using Object.assign
Using spread operator
Using a plain initialization
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 the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare three different approaches to create a new reference to an object in JavaScript: 1. `Object.assign()` 2. Spread operator (`{...}`) 3. A plain initialization using `JSON.parse()` (not recommended, but included for comparison) **Options Compared** * **Option 1: Using `Object.assign()`** + Creates a shallow copy of the original object by referencing its properties. + Returns an object with the same prototype as the original object. + Can lead to unexpected behavior if the original object has mutable properties that are also assigned to other variables. * **Option 2: Spread Operator (`{...}`)** + Creates a shallow copy of the original object by spreading its properties into a new object. + Returns an object with a separate prototype from the original object. + Is more memory-efficient than `Object.assign()`. * **Option 3: Plain Initialization using `JSON.parse()`** + Not recommended, as it can lead to unexpected behavior if the original object has complex data structures or circular references. + Creates a new object with a separate prototype from the original object. **Pros and Cons** * `Object.assign()`: + Pros: easy to use, widely supported + Cons: can lead to unexpected behavior, not memory-efficient * Spread Operator (`{...}`): + Pros: more memory-efficient, creates a new prototype + Cons: not as widely supported as `Object.assign()` * Plain Initialization using `JSON.parse()`: + Pros: None notable + Cons: not recommended due to potential unexpected behavior **Library and Purpose** There are no libraries used in this benchmark. **Special JS Feature or Syntax** The spread operator (`{...}`) is a feature introduced in ECMAScript 2018 (ES2018), but it's supported by modern browsers, including Chrome 71. The `Object.assign()` method has been part of the JavaScript standard since ECMAScript 5 (ES5). **Other Alternatives** For creating a new object with a specific prototype or behavior, other approaches can be used: * For creating an object with a specific prototype: use the constructor function (e.g., `new MyConstructor()`) * For creating an immutable object: use libraries like Immutable.js or Lodash's `createImmutable()` function * For creating an object from an array: use the spread operator (`{...array}`)
Related benchmarks:
JavaScript spread operator vs Object.assign performance (fixed)
JavaScript spread operator vs Object.assign performance v2
Spread vs Assign benchmark 2
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign vs direct performance
Comments
Confirm delete:
Do you really want to delete benchmark?