Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dynamic property assignment vs Object.assign
(version: 0)
Comparing performance of:
Direct assignment vs Object.assign
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var listOfNumbers = Array(10000).fill().reduce((acc, x, i) => (acc.push(i), acc), []);
Tests:
Direct assignment
var a = listOfNumbers.reduce((acc, x) => { acc[x] = x * 2; return acc; }, {});
Object.assign
var b = listOfNumbers.reduce((acc, x) => Object.assign(acc, { [x]: x * 2 }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Direct assignment
Object.assign
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Direct assignment
18269.8 Ops/sec
Object.assign
335.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. **What is being tested?** The benchmark compares two approaches to assign properties to an object: 1. **Direct assignment**: Using the bracket notation (`[]` syntax) to dynamically assign properties to an object, like `obj[x] = x * 2`. 2. **Object.assign**: Using the `Object.assign()` method to assign properties to an object, like `Object.assign(obj, { [x]: x * 2 })`. **Options compared** The benchmark is comparing two options: 1. Direct assignment (using bracket notation) 2. Object.assign (using a method to assign properties) **Pros and Cons of each approach:** **Direct Assignment (using bracket notation)** Pros: * Often faster and more lightweight than using `Object.assign()` * Can be less memory-intensive since it doesn't create an intermediate object * Easy to read and write, as it's a common syntax in JavaScript Cons: * May lead to errors if the property name is not a string or a number (e.g., trying to assign a value to a non-existent property) * Not supported by older browsers or environments that don't support bracket notation **Object.assign()** Pros: * More explicit and safe, as it checks for existing properties before assigning new ones * Works with most modern browsers and environments * Can be used to merge multiple objects into one Cons: * May be slower than direct assignment due to the overhead of creating an intermediate object * Requires more memory since it creates a new object on each execution **Library usage:** The benchmark doesn't use any external libraries, so no additional dependencies are involved. **Special JS feature or syntax:** This benchmark uses bracket notation (`[]` syntax) for dynamic property assignment, which is a common JavaScript syntax. There's no specific "special" feature being tested here; it's just a typical scenario that can be encountered in many JavaScript applications. **Other alternatives:** If you're looking for alternative approaches to assign properties to an object, some other options include: * Using `Object.defineProperty()` with a getter and setter * Using `Array.prototype.forEach()` or `Array.prototype.reduce()` with the `set` method (for older browsers that don't support modern methods) * Using `JSON.parse()` and `JSON.stringify()` with objects (not recommended due to performance issues) Keep in mind that these alternatives may have different trade-offs in terms of performance, memory usage, and browser compatibility.
Related benchmarks:
Object.assign vs mutate
Direct property assignment vs defineProperty
Reflect.set vs Object.assign vs Direct assignment
Reflect.set vs Object.assign vs Direct assignment vs Spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?