Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs Assign bensilverm
(version: 0)
Comparing performance of:
Spread vs Object.assign
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Person(name) { this.name = name; }
Tests:
Spread
const person = new Person("name"); const alsoPerson = {...person}
Object.assign
const person = new Person("name") const alsoPerson = Object.assign(Object.create(Object.getPrototypeOf(person)), person)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
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):
I'd be happy to explain what's being tested in the provided JSON benchmark. **Benchmark Definition** The benchmark is comparing two approaches for creating a new object by copying an existing object: using the spread operator (`...`) and using `Object.assign()`. **Options Compared** There are two options being compared: 1. **Spread Operator (`...`)**: This syntax creates a new object by taking all the key-value pairs of an existing object and adding them to a new object. For example, given an object `{a: 1, b: 2}`, `{...{a: 1, b: 2}}` would create a new object `{a: 1, b: 2}`. 2. **Object.assign()**: This method copies the properties of one or more source objects to a target object. For example, given an object `person = {name: 'John'}`, `Object.assign({}, person)` would create a new object `{name: 'John'}`. **Pros and Cons** Here are some pros and cons of each approach: **Spread Operator (`...`)** Pros: * More concise and readable * Creates a shallow copy of the original object, which can be faster for simple objects Cons: * Can create a deep copy of complex objects if not used carefully (e.g., `{...{a: 1, b: {c: 2}}}` would create a new object with nested properties) * Not supported in older browsers or Node.js versions **Object.assign()** Pros: * More flexible and powerful for creating complex copies * Supported in all modern browsers and Node.js versions Cons: * Less concise and less readable than the spread operator * Creates a shallow copy of the original object, which can be slower for simple objects **Library Used** In this benchmark, neither of the options relies on any specific library. However, `Object.create()` is used in one of the test cases to create an empty object for `Object.assign()`. `Object.getPrototypeOf()` is also used to get the prototype of the original object. **Special JS Feature/ Syntax** None of these options rely on a special JavaScript feature or syntax beyond what's described above. However, it's worth noting that modern browsers and Node.js versions support many other features and syntaxes, such as arrow functions, classes, and async/await. **Other Alternatives** Other alternatives for creating new objects by copying existing ones include: * Using the `JSON.parse()` method to create a shallow copy of an object * Using the `Array.prototype.slice()` method to create a shallow copy of an array * Using a library like Lodash or Ramda, which provide more advanced functionality for working with objects and arrays. It's worth noting that in modern JavaScript, using the spread operator is generally considered the most concise and readable way to create new objects.
Related benchmarks:
JavaScript spread operator vs Object.assign performance v2
JavaScript spread operator vs Object.assign performance without useless assignment
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance v3
Comments
Confirm delete:
Do you really want to delete benchmark?