Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs for...in... (2)
(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
target = {}; Object.assign(target, obj);
For in
target = {}; 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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.assign
3012485.5 Ops/sec
For in
519271.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches for copying properties from one object (`obj`) to another object (`target`): `Object.assign()` and the traditional `for...in` loop. **Script Preparation Code** The script preparation code creates an empty target object and an object with 100 properties, where each property has a value containing a string literal. The script is designed to make it easy for the benchmarking framework to populate the target object with properties from the source object (`obj`) in both the `Object.assign()` and `for...in` loop approaches. **Html Preparation Code** The html preparation code is empty, which means that the benchmark doesn't rely on any HTML-related setup or cleanup. **Test Cases** There are two test cases: 1. **"Object.assign"`**: This test case uses the `Object.assign()` method to copy properties from `obj` to `target`. 2. **"For in"`**: This test case uses a traditional `for...in` loop to iterate over the properties of `obj` and copy them to `target`. **Library and Purpose** * `Object.assign()`: The `Object.assign()` method is a part of the ECMAScript standard, which means it's supported by most modern browsers. It provides a convenient way to copy properties from one object to another. * `for...in` loop: This is a built-in JavaScript construct that allows iterating over the properties of an object. **Pros and Cons** * **Object.assign()**: + Pros: Fast, concise, and widely supported. + Cons: May not work as expected with certain types of objects or property names (e.g., null or undefined values). * **For in loop**: + Pros: Flexible, can handle arbitrary object properties, but may be slower due to the overhead of iterating over properties using a loop. + Cons: More verbose, may require additional setup or cleanup code. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark that wouldn't be familiar to most software engineers. However, it's worth noting that the `Object.assign()` method was introduced in ECMAScript 2015 (ES6), so if you're testing older browsers, you may need to use a polyfill or alternative implementation. **Other Alternatives** If you want to test other approaches for copying properties from one object to another, here are some alternatives: * `JSON.parse(JSON.stringify(obj))`: This method can be used to copy an entire object by serializing it as JSON and then parsing the resulting string back into a new object. However, this approach has limitations and may not work correctly with certain types of objects or property names. * Destructuring assignment: This is a syntax feature introduced in ECMAScript 2015 (ES6) that allows assigning properties from an object to variables using destructuring syntax. For example: `const { prop1, prop2 } = obj;`.
Related benchmarks:
Object.assign vs Direct Assignment #2
Object.assign vs direct assignment Mutation
Object.assign vs direct assignment Mutation2
assign vs direct
Object.assign vs for...in...
Comments
Confirm delete:
Do you really want to delete benchmark?