Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS cloning benchmark
(version: 0)
Comparing performance of:
Object.assign vs Spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Object.assign
var params = { a: 1, b:"hello", c: true, d:7, e: "test", f: "getting", g: "longer", h: false, i: "but more realistic for enterprise development" }; var other = Object.assign({ id: "5d493b95-6255-4662-8de5-875370997a7c" }, params);
Spread
var params = { a: 1, b:"hello", c: true, d:7, e: "test", f: "getting", g: "longer", h: false, i: "but more realistic for enterprise development" }; var other = { id: "5d493b95-6255-4662-8de5-875370997a7c", ...params };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
Spread
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/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.assign
13542296.0 Ops/sec
Spread
10812914.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different approaches to cloning objects in JavaScript is an interesting benchmark. **Benchmark Description** The test case measures the time taken by two approaches: 1. `Object.assign()`: This method creates a new object and copies all enumerable own properties from one or more source objects to a target object. 2. `Spread Operator (`: This syntax creates a new object with all enumerable own properties from an existing object, using the spread operator (`{ ...obj }`). **Options Compared** The benchmark compares the performance of these two approaches: * **Object.assign()**: Pros: + Well-supported in most browsers and JavaScript engines. + Can handle nested objects. + Returns a new object with all properties from the source object. * **Spread Operator (`)**: Cons: + Only supported in modern browsers (Chrome 109) and some Node.js versions. + May have performance overhead due to syntax parsing. + Only works for shallow cloning, not handling nested objects. **Pros and Cons of Each Approach** * **Object.assign()**: + Pros: widely supported, efficient for shallow cloning. + Cons: can be verbose with multiple source objects. * **Spread Operator (`)**: + Pros: concise syntax, easier to read. + Cons: limited support, may have performance overhead. **Library Usage** In the provided benchmark definition, there is no explicit library usage. However, if a library like Lodash or Underscore.js were used, it might affect the cloning behavior and performance. **Special JS Features or Syntax** The test case uses the spread operator (`)** for cloning objects, which is a relatively recent JavaScript feature (introduced in ECMAScript 2018). This syntax is supported in modern browsers and some Node.js versions but may not work in older environments. **Other Alternatives** If `Object.assign()` or the spread operator (`)** are not suitable options, other alternatives for cloning objects include: * Using a library like Lodash's `cloneDeep()` * Implementing a custom cloning function using recursion or iteration * Using a JavaScript framework or utility library that provides an efficient cloning mechanism In summary, the benchmark measures the performance of two approaches to cloning objects in JavaScript: `Object.assign()` and the spread operator (`)**. The choice of approach depends on the specific requirements, such as supporting older browsers or handling nested objects.
Related benchmarks:
JavaScript spread operator vs cloneDeep
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (large array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
JS Cloning benchmarking
Object Cloning Performance Benchmark: Spread vs. Object.assign and More
Comments
Confirm delete:
Do you really want to delete benchmark?