Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.defineProperty vs Object.assign
(version: 0)
Comparing performance of:
Object.defineProperty vs Object.assign (immutable) vs Object.assign (mutable)
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var input = new Array(100).fill().map((v, i) => i)
Tests:
Object.defineProperty
const output = input.reduce((acc, value) => Object.defineProperty(acc, `key${value}`, { value }), {});
Object.assign (immutable)
const output = input.reduce((acc, value) => Object.assign({}, 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.defineProperty
Object.assign (immutable)
Object.assign (mutable)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.defineProperty
66523.1 Ops/sec
Object.assign (immutable)
1822.2 Ops/sec
Object.assign (mutable)
61304.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is comparing three approaches for defining properties on an object: 1. `Object.defineProperty`: This method sets one or more own properties on an object, creating them if they don't exist. It can also be used to add new attributes to existing objects. 2. `Object.assign` (with mutable assignment): This method copies all enumerable own properties from one or more source objects to a target object. 3. `Object.assign` (with immutable assignment): This method creates a shallow copy of an array, which is then used as the initial value for the `acc` object in the reduction. **Options Compared** The benchmark is comparing the performance of these three approaches: * **Immutable Object Assign**: Using `Object.assign` to create a new object with properties set from another object. This approach creates a new object and copies the properties, which can be slower. * **Mutable Object Assign**: Using `Object.assign` on an existing object to add new properties. This approach modifies the original object, which can be faster since it avoids creating a new object. * **DefineProperty**: Using `Object.defineProperty` to set properties on an object. This method creates the property if it doesn't exist and can also modify existing ones. **Pros and Cons of Each Approach** 1. **Immutable Object Assign (with Object.assign)**: * Pros: Creates a new object, which can be beneficial for data protection. * Cons: Can be slower due to the overhead of creating a new object. 2. **Mutable Object Assign (with Object.assign)**: * Pros: Faster since it modifies an existing object without creating a new one. * Cons: May not be suitable for data protection, as changes can be lost if the original object is modified elsewhere. 3. **DefineProperty**: * Pros: Can set properties on an existing object and create them if they don't exist. * Cons: Can be slower due to the overhead of setting properties on an object. **Library Usage** None of the test cases use a specific library, but it's worth noting that `Object.assign` is part of the ECMAScript standard, while `Object.defineProperty` uses a proprietary method call. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in these benchmark definitions. The focus is on comparing the performance of different object assignment approaches. **Other Alternatives** If you're looking for alternative ways to assign properties to an object, you could consider: 1. **Object.create()**: Creates a new object with a given prototype. 2. **Array.prototype.forEach()**: Iterates over an array and executes a callback function for each element. 3. **for...of loop**: Iterates over an iterable (e.g., an array) using a for-of loop. However, the current benchmark is specifically focused on comparing `Object.defineProperty` with `Object.assign`, making these alternatives less relevant to this particular comparison.
Related benchmarks:
Object.defineProperty vs Object.assign vs key set
Object.defineProperty vs Object.assign vs. Direct property assignment
Object.defineProperty vs Object.assign vs direct assigment
Object.defineProperty vs Object.assign vs Object spread
Comments
Confirm delete:
Do you really want to delete benchmark?