Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for of vs object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.keys - for loop vs for of - Object.keys vs for of - Object.entries
Created:
3 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 };
Tests:
for-in
let result; for (var i=10000; i > 0; i--) { for (var key in obj) { result += obj[key]; } } console.log(result);
Object.keys
let result; for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => { result += obj[key]; }); } console.log(result);
Object.keys - for loop
let result; for (var i=10000; i > 0; i--) { const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { result += obj[keys[i]]; }; } console.log(result);
for of - Object.keys
let result; for (var i=10000; i > 0; i--) { for (const key of Object.keys(obj)) { result += obj[key]; } } console.log(result);
for of - Object.entries
let result; for (var i=10000; i > 0; i--) { for (const [key, value] of Object.entries(obj)) { result += value; } } console.log(result);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.keys - for loop
for of - Object.keys
for of - Object.entries
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 world of JavaScript microbenchmarks. The provided benchmark measures the performance of three different approaches for iterating over an object's keys: 1. `for-in` 2. `Object.keys` with a traditional `forEach` loop 3. `Object.keys` with a `for...of` loop **For-in** The `for-in` loop iterates over the object's own enumerable properties (i.e., keys). This approach is straightforward but has some limitations: Pros: * Easy to use and understand for beginners * Works well for simple iterations Cons: * Can iterate over inherited properties, which might not be desired in all cases * Might perform more work than necessary due to the iteration over prototype chain * Has performance overhead compared to other approaches **Object.keys** This approach uses the `Object.keys()` method to get an array of the object's own enumerable keys. The test then loops through this array using a traditional `forEach` loop: Pros: * Efficient and accurate way to iterate over an object's keys * Works well with modern JavaScript versions Cons: * Requires a separate call to `Object.keys()`, which might add overhead * Not suitable for complex or deeply nested objects, as it doesn't handle inherited properties **Object.keys - For loop** This approach is similar to the previous one, but uses a traditional `for` loop instead of a `forEach` loop: Pros: * Can be slightly more efficient than the `forEach` loop version * Still provides an accurate way to iterate over an object's keys Cons: * Requires manual index management, which can lead to errors or off-by-one issues if not handled correctly * Not as concise or readable as the `for...of` loop version **For of - Object.keys** This approach uses a combination of `Object.keys()` and a `for...of` loop. The `for...of` loop iterates over an array (in this case, the result of `Object.keys()`) and executes a block of code for each iteration: Pros: * Provides an efficient way to iterate over an object's keys with minimal overhead * Works well with modern JavaScript versions Cons: None notable **Other considerations** When writing benchmark tests like these, it's essential to consider the following factors: * **Array creation**: Are arrays created during the test? If so, this can impact performance. * **Prototype chain**: Does the object have a prototype chain? This might affect iteration performance. * **ES version support**: Are the tests optimized for specific ES versions or are they compatible with all modern browsers? * **Benchmarking library**: Is a specialized benchmarking library used, or is it a simple script like this one? **Alternatives** If you're looking for alternatives to `for-in`, `Object.keys` with traditional loops, and `for...of` loops, consider the following approaches: 1. **Using `for...in` with a simple loop**: This can be faster than traditional loops for iterating over an object's keys. 2. **Utilizing `Symbol.iterator`**: Objects implementing `Symbol.iterator` provide a more efficient way to iterate over their contents. 3. **Checking for array-like objects**: If the test is only interested in iterating over numeric values, it might be possible to skip iteration altogether. Keep in mind that the best approach depends on your specific use case and requirements. Always profile and benchmark your code to determine the most efficient solution for your particular problem.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
Object.entries vs Object.keys vs for...in
for-in vs for object.keys keys
Comments
Confirm delete:
Do you really want to delete benchmark?