Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs direct assignment multiple properties
(version: 0)
Comparing performance of:
Direct Assignment vs Object.assign
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = {};
Tests:
Direct Assignment
for(var i = 0; i < 10000; i++) { data[`prop_${i}`] = true; data[`prop_${i}1`] = true; data[`prop_${i}2`] = true; data[`prop_${i}3`] = true; data[`prop_${i}4`] = true; data[`prop_${i}5`] = true; data[`prop_${i}6`] = true; data[`prop_${i}7`] = true; data[`prop_${i}8`] = true; data[`prop_${i}9`] = true; data[`prop_${i}10`] = true; data[`prop_${i}11`] = true; data[`prop_${i}12`] = true; }
Object.assign
for(var i = 0; i < 10000; i++) { var propName = `prop_${i}`; Object.assign(data, { [propName]: true }); }
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Direct Assignment
60.4 Ops/sec
Object.assign
240.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark compares two approaches for assigning multiple properties to an object: `Object.assign` and direct assignment (also known as "chaining" or "dot notation"). The test creates a large array of objects with 10 properties each, assigns the value `true` to each property using both methods, and measures their performance. **What is being tested?** The benchmark tests the performance difference between using the `Object.assign()` method to assign multiple properties to an object versus directly assigning each property individually (using dot notation or "chaining"). **Options Compared:** 1. **Direct Assignment**: Assigning individual properties to an object using dot notation (`data.prop_name = true;`). 2. **Object.assign() Method**: Using the `Object.assign()` method to assign multiple properties to an object in a single operation. **Pros and Cons of each approach:** * **Direct Assignment**: + Pros: Simple, concise syntax. + Cons: Can be slower due to repeated property lookups on the target object. * **Object.assign() Method**: + Pros: Efficient for assigning multiple properties in a single operation, reducing overhead due to repeated property lookups. + Cons: Requires creating an object with the desired properties and using the `assign()` method, which can be more verbose. In general, the `Object.assign()` method is preferred when assigning large numbers of properties, as it reduces the number of property lookups on the target object. However, for small datasets or performance-critical code, direct assignment might still be a viable option due to its simplicity and concise syntax. **Library and Purpose:** In this benchmark, `Object.assign()` is a built-in JavaScript method that allows you to copy properties from one or more source objects to a target object. **Special JS feature or syntax:** There are no special features or syntaxes mentioned in the provided benchmark. **Alternative approaches:** Other approaches for assigning multiple properties to an object include: * Using the spread operator (`{ ...obj }`). * Utilizing `Object.create()` and `Object.assign()` together. * Employing library functions like Lodash's `assignIn()` or Moment.js's `assign()`. While these alternatives might offer similar performance benefits as the `Object.assign()` method, they may also introduce additional overhead due to function calls or require additional libraries.
Related benchmarks:
object.assign vs spread to create a copy
JS Object assign vs object new property 2
object spread vs Object.assign
Object.assign vs mutation
assign vs direct 2
Comments
Confirm delete:
Do you really want to delete benchmark?