Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object creation vs assign
(version: 0)
Comparing performance of:
New object vs Spread vs Assign
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
New object
function createObject(total) { return { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1, g: 1, h: 1, i: 1, j: 1, k: 1, l: 1, total: total } } createObject(1)
Spread
const firstObject = { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1, g: 1, h: 1, i: 1, j: 1, k: 1, l: 1, } function createObject(total) { return { ...firstObject, total: total } } createObject(1)
Assign
const firstObject = { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1, g: 1, h: 1, i: 1, j: 1, k: 1, l: 1, } function createObject(total) { return Object.assign({ total: total }, firstObject); } createObject(1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
New object
Spread
Assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
New object
84132600.0 Ops/sec
Spread
19187132.0 Ops/sec
Assign
2231241.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The website MeasureThat.net allows users to create and run JavaScript microbenchmarks. The current benchmark is focused on comparing three different approaches for creating objects with similar properties: "New object", "Spread", and "Assign". **Test Cases** There are three individual test cases: 1. **"New object"`** This test case creates a new object using the `createObject` function, which returns an object with 11 properties (including a repeating property `total`) and initializes all of them with a value of 1. 2. **"Spread"`** This test case uses the spread operator (`...`) to create a new object by merging the existing object `firstObject` with another object that contains only the `total` property, initialized with a value of 1. The resulting object will have all properties from `firstObject` and the additional `total` property. 3. **"Assign"`** This test case uses the `Object.assign()` method to create a new object by merging two objects: the existing object `firstObject` and another object that contains only the `total` property, initialized with a value of 1. **Library Used** In the "Spread" test case, the library used is the spread operator (`...`). This operator is introduced in ECMAScript 2018 (ES2018) as part of the Object Spread Operator. It allows you to create a new object by merging one or more source objects into an existing target object. **Special JS Feature/Syntax** The "Spread" test case uses the spread operator (`...`), which is a relatively recent addition to JavaScript. While it's widely supported in modern browsers, older browsers may not support this syntax. **Pros and Cons of Different Approaches** Here are some pros and cons of each approach: 1. **"New object"`**: * Pros: Simple and straightforward way to create an object with default values. * Cons: May be slower due to the overhead of creating a new object and assigning default values. 2. **"Spread"`**: * Pros: More concise way to merge objects, potentially faster due to reduced property assignment overhead. * Cons: May not work in older browsers that don't support the spread operator, and requires understanding of this syntax. 3. **"Assign"`**: * Pros: Standard method for merging objects, widely supported across browsers and JavaScript versions. * Cons: Can be slower due to the overhead of creating a new object and assigning values. **Other Alternatives** If you're interested in exploring alternative approaches, here are some additional options: 1. **Using `Object.create()`**: You can create an object using `Object.create()`, which allows you to specify a prototype for the new object. This method is similar to "New object" but provides more flexibility. 2. **Using a class-based approach**: You can define a class that creates objects with default properties, which can be used as an alternative to "New object". Keep in mind that these alternatives may not provide performance benefits comparable to the tested approaches. I hope this explanation helps! Let me know if you have any further questions or need clarification on any points.
Related benchmarks:
JS object copy spread vs assign
Object.assign vs direct copy
Object assign vs empty obj
Object.assign() vs Reflect.set()
JavaScript: Normal assignation VS Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?