Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object initialization direct vs. spread vs. Object.assign
(version: 0)
Comparing performance of:
Direct vs Spread vs Object.assign
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
let props = [ "astId", "astCode", "astDescription", "sttId", "sttValue", "sttDescription", "asttId", "asttDescription", "astpId", "astpCode", "astpDescription", "astpMake", "astpModel", "astSerialNumber", "astAcquiredOn", "astInstalledOn", "astWarrantyExpiresOn", "pltId", "astIdParent", "astCodeParent", "astDescriptionParent", "pltPlantCode", "pltDescription", "wrcId", "wrcShortDesc", "wrcDescription", "mcnCode", "mcnDescription", "mchtDescription", "astIdAstAv", "purchaser", "purchase Date", ]; let empty = {}; props.forEach(prop => empty[prop] = null); function createEmptyRow() { const result = {}; props.forEach(prop => result[prop] = null); return result; } function copyEmptyRow() { return { ...empty } } function copyEmptyRowAssign() { return Object.assign({}, empty); }
Tests:
Direct
var data = createEmptyRow()
Spread
var data = copyEmptyRow()
Object.assign
var data = copyEmptyRowAssign()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Direct
Spread
Object.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; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Direct
379109.8 Ops/sec
Spread
974225.7 Ops/sec
Object.assign
5191698.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark. **Benchmark Overview** The benchmark tests three different ways to create an empty object in JavaScript: directly using an empty object literal, using the spread operator (`...`), and using `Object.assign()`. **Options Compared** * **Directly using an empty object literal**: `let result = {};` * **Using the spread operator (`...`)**: `{ ...empty }` * **Using `Object.assign()`**: `Object.assign({}, empty)` **Pros and Cons of Each Approach:** 1. **Directly using an empty object literal**: * Pros: Fast, simple, and concise. * Cons: May not work in older browsers that don't support object literals or the spread operator. 2. **Using the spread operator (`...`)**: * Pros: Fast, modern, and widely supported (including in Firefox 129). * Cons: Introduced with ECMAScript 2018, so may require transpilation for older browsers. 3. **Using `Object.assign()`**: * Pros: Works in all browsers that support JavaScript, including older ones. * Cons: Generally slower and more verbose than the other two options. **Library Usage** None of the benchmark test cases use any external libraries. The only library used is `props`, an array of property names used to create the empty object. **Special JS Features or Syntax** The benchmark uses the spread operator (`...`), which was introduced in ECMAScript 2018 (ES2018). It also uses `Object.assign()`, which has been a part of JavaScript since its inception. There are no special features or syntaxes used beyond these two. **Other Alternatives** In addition to the three options compared, there is another way to create an empty object using the bitwise NOT operator (`!`) with the `Object` constructor: * `let result = new Object(!);` This method creates a new, empty object by negating the zero value of the Boolean type, which is 0. However, this approach is generally considered less readable and less reliable than the other three options. In summary, the benchmark compares the performance of three ways to create an empty object in JavaScript: directly using an object literal, using the spread operator, and using `Object.assign()`. The results show that all three approaches are fast and widely supported, but `Object.assign()` is generally slower.
Related benchmarks:
object.assign vs spread to create a copy
JavaScript spread operator vs Object.assign performance vs property add
JavaScript spread operator vs Object.assign performance without useless assignment
JavaScript spread operator vs Object.assign performance vs Multiple statementd
JavaScript spread operator vs Object.assign performance create new
Comments
Confirm delete:
Do you really want to delete benchmark?