Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Plain object, for vs for in vs for of
(version: 0)
Comparing performance of:
for loop vs for of vs for in
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = { first: 'john', last: 'smith', address: { street: 'street street' } }; var b = { first: 'j', last: 's' };
Tests:
for loop
const props = Object.keys(a); const length = props.length; let hasNewKey = false; for (let i = 0; i < length; i++) { const key = props[i]; if (!(key in b)) { hasNewkey = true; } }
for of
const props = Object.keys(a); let hasNewKey = false; for (const key of props) { if (!(key in b)) { hasNewkey = true; } }
for in
let hasNewKey = false; for (const key in a) { if (!(key in b)) { hasNewkey = true; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for loop
for of
for in
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark compares the performance of three different approaches for iterating over an object's keys in JavaScript: `for`, `for in`, and `for of`. The test cases use a plain object `a` with multiple properties, including another nested object `address`. **Options Compared** 1. **For Loop**: A traditional loop using the `for` keyword. 2. **For In Loop**: A loop that iterates over an object's own enumerable properties using the `in` operator. 3. **For Of Loop**: A newer loop introduced in ECMAScript 2015, which allows iterating over iterable objects (like arrays or plain objects) without using the `for` keyword. **Pros and Cons** * **For Loop**: + Pros: Simple, widely supported, and easy to understand. + Cons: May not be as efficient as other options for large datasets or when iterating over specific properties. * **For In Loop**: + Pros: Allows iteration over object keys, but can be less efficient than `for` loops due to the need to check if a property is enumerable. + Cons: Can lead to unexpected behavior if not used carefully (e.g., iterating over inherited properties). * **For Of Loop**: + Pros: Efficient and concise way to iterate over iterable objects, making it suitable for large datasets or complex object structures. + Cons: Requires ECMAScript 2015 support, which may limit its applicability. **Library/ Syntax Considerations** None of the test cases use a specific library. However, the `for in` loop option might be affected by the presence of inherited properties if the object has prototype chains. **Special JS Feature/Syntax** The `For Of Loop` option uses a new JavaScript feature introduced in ECMAScript 2015, allowing iteration over iterable objects without using the `for` keyword. This syntax is relatively modern and might not be supported by older browsers or versions of JavaScript. **Other Alternatives** In addition to the mentioned options, other approaches for iterating over object keys include: 1. Using the `Object.keys()` method (as shown in one of the test cases). 2. Utilizing a library like Lodash's `keys()` function. 3. Implementing a custom iteration mechanism using `forEach` and checking if each property is present in the target object. Keep in mind that these alternatives might offer different trade-offs in terms of performance, readability, or compatibility with older browsers.
Related benchmarks:
For in vs For of
for in vs for of
object destruction vs. dot notation 2
object spread vs Object.assign
Plain object, for vs for in vs for of vs while
Comments
Confirm delete:
Do you really want to delete benchmark?