Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs for...in...
(version: 0)
Comparing performance of:
Object.assign vs For in
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var target = {}; var obj = {}; for (var i = 0; i < 100; i++) { obj['propp'+i] = ('this is a value ' + i); }
Tests:
Object.assign
Object.assign(target, obj);
For in
for (var key in obj) { target[key] = obj[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
For in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
28 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Goanna/6.8 PaleMoon/34.0.1
Browser/OS:
Pale Moon (Firefox Variant) 34 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.assign
23215.3 Ops/sec
For in
27326.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches: `Object.assign` and `for...in`. The benchmark aims to measure which approach is faster for assigning properties to an object. **Options Compared** 1. **Object.assign**: This method takes two arguments, the target object and the source object. It copies all enumerable own properties from the source object to the target object. 2. **For-in Loop**: This loop iterates over each property in the `obj` object and assigns its value to a corresponding property in the `target` object. **Pros and Cons** * **Object.assign** + Pros: - Concise and readable syntax - Efficient, as it uses native JavaScript optimized code + Cons: - Less control over the assignment process - May not work correctly if source object has non-enumerable properties * **For-in Loop** + Pros: - More control over the assignment process (e.g., skipping certain properties) - Works with all objects, including those with non-enumerable properties + Cons: - Less concise and less readable syntax - May be slower due to iteration overhead **Library and Special JS Features** There is no library or special JavaScript feature mentioned in this benchmark. The focus is solely on comparing two basic approaches for object assignment. **Other Alternatives** In addition to `Object.assign` and the `for-in` loop, other alternatives might include: * Using a library like Lodash's `assignIn` method * Using a function like `forEach` or `map` with an arrow function * Using a more modern approach with object spread syntax (`{ ...target, ...obj }`) * Using a different iteration mechanism, such as `for...of` or `while` Keep in mind that the choice of alternative will depend on the specific requirements and constraints of your use case. **Benchmark Preparation Code** The provided script preparation code creates two objects: `target` and `obj`. The `target` object is empty, while the `obj` object has 100 properties named `proppX`, where X ranges from 0 to 99. Each property is assigned a string value constructed by concatenating 'this is a value ' with the current iteration number. The HTML preparation code is null, indicating that no specific HTML setup or initialization is required for this benchmark. **Latest Benchmark Result** The latest benchmark result shows two test cases: one running the `for-in` loop and another running the `Object.assign` method. The results indicate that the `for-in` loop is faster than `Object.assign`, with execution times per second being 861,590.81 for `For in` and 532,681.38 for `Object.assign`.
Related benchmarks:
Object.assign vs Direct Assignment #2
Object.assign vs direct assignment Mutation
Object.assign vs direct assignment Mutation2
assign vs direct
Comments
Confirm delete:
Do you really want to delete benchmark?