Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.defineProperties with getOwnPropertyDescriptors vs Object.assign vs Object spread
(version: 0)
Comparing performance of:
Object.defineProperties vs Object spread vs Object.assign (mutable)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var input = new Array(100).fill().map((v, i) => i)
Tests:
Object.defineProperties
const output = input.reduce((acc, value) => Object.defineProperties(acc, Object.getOwnPropertyDescriptors({[`key${value}`]: value})), {});
Object spread
const output = input.reduce((acc, value) => ({ ...acc, [`key${value}`]: value }));
Object.assign (mutable)
const output = input.reduce((acc, value) => Object.assign(acc, { [`key${value}`]: value }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.defineProperties
Object spread
Object.assign (mutable)
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.defineProperties
14362.9 Ops/sec
Object spread
7997.1 Ops/sec
Object.assign (mutable)
40612.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explain what's being tested. **Benchmark Description** The benchmark is designed to compare three approaches for creating objects with dynamic properties: 1. `Object.defineProperties` (with `getOwnPropertyDescriptors`) 2. `Object.assign` 3. `Object spread` These approaches are used to create an object with 100 properties, where each property's key is a unique integer value. **Options Compared** Here's a brief overview of the three options being compared: 1. **`Object.defineProperties` (with `getOwnPropertyDescriptors`)** * Pros: + Allows for more fine-grained control over property creation and access. + Can be used to create objects with complex property hierarchies. * Cons: + Can lead to slower performance due to the overhead of accessing property descriptors. 2. **`Object.assign`** * Pros: + Fast and efficient, as it uses a simple array concatenation approach. * Cons: + Less flexible than `Object.defineProperties`, as it only allows for addition of new properties. 3. **`Object spread`** (using the spread operator (`{ ... }`)) * Pros: + Fast and efficient, similar to `Object.assign`. + Allows for more concise code than traditional object creation methods. **Library Used** None of the options rely on external libraries. **Special JS Feature/Syntax** The benchmark uses the spread operator (`{ ... }`) in one of the test cases. This is a modern JavaScript feature introduced in ECMAScript 2018 (ES8). The spread operator allows for creating new objects by copying properties from an existing object, while also allowing for adding new properties. **Benchmark Preparation Code** The preparation code creates an array of 100 integers using `Array(100).fill().map((v, i) => i)`. This code is used to generate the dynamic property keys and values. **Latest Benchmark Result** The benchmark results show that: * `Object.assign (mutable)` has the fastest execution time (40612.24609375 executions per second). * `Object.defineProperties` comes in second (14362.876953125 executions per second). * `Object spread` is slower than both of these options (7997.0869140625 executions per second). **Other Alternatives** If you're interested in exploring other approaches, here are a few alternatives: 1. Using `Object.create()` with custom property descriptors. 2. Utilizing a library like Lodash's `assignIn` function to create objects with dynamic properties. 3. Implementing a custom object creation function using bitwise operators or recursion. Keep in mind that these alternatives may introduce additional complexity or performance overhead, depending on your specific use case.
Related benchmarks:
Object.assign vs spread operatora
object.assign vs spread to create a copy
object spread vs Object.assign
spread operator vs object assign vs object setPrototypeOf vs reflect setPrototypeOf
spread operator vs object assign vs object setPrototypeOf vs reflect setPrototypeOf v2
Comments
Confirm delete:
Do you really want to delete benchmark?