Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs object assign
(version: 0)
Comparing performance of:
Object.assign vs spread operator vs object assign
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Object.assign
var params = { b:"hello", c: true, d:7 }; var other = Object.assign({ a: 2 }, params);
spread operator
var params = { b:"hello", c: true, d:7 }; var other = { a: 2, ...params };
object assign
var params = { b:"hello", c: true, d:7 }; params.a = 2; var other = params;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.assign
spread operator
object assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.assign
5725621.5 Ops/sec
spread operator
18785156.0 Ops/sec
object assign
1299812608.0 Ops/sec
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 defined in two parts: 1. **Script Preparation Code**: This section is empty, which means that the script to be executed for each test case is not provided. The script will be generated automatically by MeasureThat.net. 2. **Html Preparation Code**: This section is also empty, indicating that no HTML code is required for the benchmark. **Individual Test Cases** There are three individual test cases: 1. **Object.assign** ```javascript var params = { b: "hello", c: true, d: 7 }; var other = Object.assign({ a: 2 }, params); ``` This test case creates an object `params` with properties `b`, `c`, and `d`. Then, it uses the `Object.assign()` method to create another object `other` by merging the `params` object with an initial object `{ a: 2 }`. Pros: * This approach is straightforward and easy to understand. * It allows for efficient merging of objects. Cons: * Performance might be affected if the merged object is large. * The `Object.assign()` method creates a new object, which can lead to memory allocation issues. 2. **Spread Operator** ```javascript var params = { b: "hello", c: true, d: 7 }; var other = { a: 2, ...params }; ``` This test case creates an object `params` with properties `b`, `c`, and `d`. Then, it uses the spread operator (`...`) to create another object `other` by copying the properties of `params` into an initial object `{ a: 2 }`. Pros: * The spread operator is more memory-efficient than `Object.assign()`. * It creates a new object without the overhead of method calls. Cons: * This approach might be less intuitive for developers unfamiliar with the spread operator. * Some older browsers may not support the spread operator. 3. **Object Assign (manual assignment)** ```javascript var params = { b: "hello", c: true, d: 7 }; params.a = 2; var other = params; ``` This test case creates an object `params` with properties `b`, `c`, and `d`. Then, it manually assigns a value to the `a` property of `params` and copies the entire `params` object into another variable `other`. Pros: * This approach is simple and easy to understand. * It can be useful for developers who prefer manual assignment. Cons: * Performance might be slower than using `Object.assign()` or the spread operator. * This approach involves more code duplication. **Libraries Used** None of the test cases explicitly use any libraries. However, if we analyze the code, we can see that `Object.assign()` is a built-in JavaScript method. **Special JS Features/Syntax** The spread operator (`...`) was introduced in ECMAScript 2018 (ES10). It's a new feature that allows creating objects or arrays from iterables. This feature is widely supported in modern browsers and Node.js environments. In summary, the benchmark tests three approaches to assigning properties to an object: 1. Using `Object.assign()` 2. Using the spread operator (`...`) 3. Manual assignment Each approach has its pros and cons, which are discussed above. The spread operator is a more memory-efficient alternative to `Object.assign()`, but it might be less intuitive for developers unfamiliar with this feature.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
Object.assign vs direct copy
Object assign vs empty obj
JavaScript: Normal assignation VS Object.assign
Object.assign vs mutation
Comments
Confirm delete:
Do you really want to delete benchmark?