Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..in vs for loop with Object.keys
(version: 0)
Comparing performance of:
for-in vs for keys
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1, 'j': 1, 'k': 1, 'l': 1, 'm': 1, 'n': 1, 'o': 1, 'p': 1, 'q': 1, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 1, 'w': 1, 'x': 1, 'y': 1, 'z': 1, 'aa': 1, 'ba': 1, 'ca': 1, 'da': 1, 'ea': 1, 'fa': 1, 'ga': 1, 'ha': 1, 'ia': 1, 'ja': 1, 'ka': 1, 'la': 1, 'ma': 1, 'na': 1, 'oa': 1, 'pa': 1, 'qa': 1, 'ra': 1, 'sa': 1, 'ta': 1, 'ua': 1, 'va': 1, 'wa': 1, 'xa': 1, 'ya': 1, 'za': 1, 'ab': 1, 'bb': 1, 'cb': 1, 'db': 1, 'eb': 1, 'fb': 1, 'gb': 1, 'hb': 1, 'ib': 1, 'jb': 1, 'kb': 1, 'lb': 1, 'mb': 1, 'nb': 1, 'ob': 1, 'pb': 1, 'qb': 1, 'rb': 1, 'sb': 1, 'tb': 1, 'ub': 1, 'vb': 1, 'wb': 1, 'xb': 1, 'yb': 1, 'zb': 1 };
Tests:
for-in
for (let i=100; i > 0; i--) { for (const key in obj) { console.log(key); } }
for keys
var keys = Object.keys(obj); for (let i=100; i > 0; i--) { for (let j = 0; j < keys.length; j ++) { console.log(keys[j]); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
for keys
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):
Let's dive into the benchmark and explain what's being tested. **Benchmark Definition** The benchmark measures the performance difference between two approaches: `for...in` loop and using `Object.keys()` to iterate over an object. **Options Compared** 1. **For...in Loop**: This approach uses a traditional `for` loop with an `in` keyword to iterate over the properties of an object. The loop variable (`key`) takes on each property name in sequence, allowing for direct access to the property values. 2. **For Keys Loop with Object.keys()**: This approach uses `Object.keys()` to get an array of property names from the object, and then iterates over this array using a traditional `for` loop. **Pros and Cons** 1. **For...in Loop**: * Pros: Can directly access property values without indexing, often more readable for simple cases. * Cons: May iterate over inherited properties (not explicitly listed in the object), can be slower due to potential array creation on older browsers. 2. **For Keys Loop with Object.keys()**: * Pros: Avoids iterating over inherited properties, generally faster due to avoidance of potential array creation on older browsers. * Cons: Requires explicit use of `Object.keys()` and array iteration, may lead to less readable code. **Library Used** In this benchmark, `Object.keys()` is used from the JavaScript Standard Library. Its purpose is to return an array-like object containing all property names (keys) available in the specified object. **Special JS Feature/Syntax** This benchmark does not use any special features or syntax like async/await, generators, or ES6 modules. **Alternatives** Other alternatives for iterating over objects in JavaScript include: 1. **Using `for...of` loop**: This approach uses a traditional `for` loop with an `of` keyword to iterate directly over property names (similar to the "For Keys Loop" option). 2. **Using `Array.prototype.forEach()` or `Array.prototype.forEach.call()`**: These approaches use array methods to iterate over property names, which can be more readable but may not always provide direct access to property values. In summary, this benchmark measures the performance difference between two common ways of iterating over objects in JavaScript: using a traditional `for...in` loop versus using `Object.keys()` and explicit array iteration.
Related benchmarks:
for-in vs object.keys with for loop large obj fixed
for-in vs object.keys with for
for..in vs for loop using Object.keys
Object.keys vs for-in
Comments
Confirm delete:
Do you really want to delete benchmark?