Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs for-in assign
(version: 0)
Comparing performance of:
Object.assign vs for-in assign
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var input = {}; for (let i = 0; i < 100_000; ++i) { input[`prop${i}`] = i; } var output = {};
Tests:
Object.assign
Object.assign(output, input);
for-in assign
for (let prop in input) { output[prop] = input[prop]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
for-in assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.assign
293.6 Ops/sec
for-in assign
154.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two approaches for assigning properties to an object: `Object.assign()` and a `for-in` loop. The test creates a large input object with 100,000 properties, each containing a unique value from 0 to 99,999. The output object is also created empty. **Options Compared** The benchmark tests the performance of two options: 1. **Object.assign()**: This method takes two arguments: the source object and the target object. It assigns all enumerable own properties from the source object to the target object. 2. **for-in loop**: This approach uses a `for...in` loop to iterate over the input object's properties, assigning each property value to the corresponding key in the output object. **Pros and Cons of Each Approach** 1. **Object.assign()**: * Pros: Concise, readable, and widely supported. * Cons: Can be slower due to the overhead of method calls and potential optimizations in other JavaScript implementations. 2. **for-in loop**: * Pros: Low-level control, can be optimized for performance, and is often preferred by developers familiar with the `for...in` syntax. * Cons: Less readable and more prone to errors, especially when dealing with large numbers of properties. **Library Used** None, as this benchmark only uses built-in JavaScript functions. **Special JS Feature/Syntax (Not Applicable)** No special features or syntax are used in this benchmark. **Alternative Approaches** Other ways to assign properties to an object include: 1. **Array.prototype.map()**: Assigns values to keys using the `map()` method and an arrow function. 2. **Array.prototype.forEach() with Object.keys()**: Uses `forEach()` and `Object.keys()` to iterate over the input object's properties and assign them to the output object. 3. **Destructuring assignment**: Uses destructuring syntax to assign properties from the input object to the output object. **Benchmark Preparation Code Explanation** The script preparation code creates an empty input object (`input`) and populates it with 100,000 properties using a `for...in` loop. The code then defines an empty output object (`output`). This setup allows both benchmarked methods to be tested in isolation. **Individual Test Cases** Each test case contains a single benchmark definition: 1. **Object.assign()**: Assigns the input object to the output object using `Object.assign()`. 2. **for-in loop**: Iterates over the input object's properties and assigns each value to the corresponding key in the output object using a `for...in` loop. **Latest Benchmark Result** The results show the number of executions per second for each browser (Firefox 125) on a desktop, Linux device: * **Object.assign()**: 293.5997619628906 executions/second * **for-in assign**: 154.36810302734375 executions/second This result suggests that `Object.assign()` is significantly faster than the `for-in` loop approach in this specific benchmark.
Related benchmarks:
Object.assign vs Direct Assignment #2
Object.assign vs direct assignment Mutation
Object.assign vs direct assignment (Improved cases)
assign vs direct
Comments
Confirm delete:
Do you really want to delete benchmark?