Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test loops
(version: 0)
Comparing performance of:
for-in vs Object.keys vs for
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 };
Tests:
for-in
for (var key in obj) { console.log(obj[key]); }
Object.keys
Object.keys(obj).forEach(key => console.log(obj[key]));
for
for (var i = 0, keys = Object.keys(obj); i < keys.length; i++) { console.log(obj[keys[i]]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
for
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 break down the provided benchmarking test cases. **Overview** The goal of this benchmark is to measure the performance of three different ways to iterate over an object in JavaScript: using `for-in`, `Object.keys()`, and manual indexing with a loop. The benchmark prepares a simple object with 7 properties, each initialized to 1. **What's being tested?** Each test case measures how many iterations per second (ExecutionsPerSecond) are performed by different browsers (Chrome 89) on a desktop platform using the specified iteration method. **Options compared:** 1. **For-in**: Iterates over an object's properties using the `for-in` loop. 2. **Object.keys()**: Returns an array of the object's property names and iterates over it using `forEach()` or manual indexing with a loop. 3. **Manual indexing with for-loop**: Uses a traditional `for` loop to iterate over the array of property names. **Pros and cons:** 1. **For-in**: * Pros: Simple and concise syntax. * Cons: Can be slower due to its own internal logic and potentially accessing inherited properties. 2. **Object.keys()**: * Pros: Modern, efficient, and well-documented. * Cons: May be slower for large objects due to the overhead of creating an array of property names. 3. **Manual indexing with for-loop**: * Pros: Fast, lightweight, and controllable. * Cons: Requires more code and manual maintenance. **Library/Function used:** In the benchmark, `Object.keys()` is a built-in JavaScript function that returns an array-like object containing the property names of an object. The other two options rely on standard JavaScript syntax and do not require any external libraries. **Special JS feature/syntax:** None mentioned in this specific benchmark. However, it's worth noting that some modern browsers (e.g., Edge) support additional features like `for...of` loops or `Map.prototype.forEach()` which could be used for similar iterations, but are not used here. **Alternatives:** Other alternatives to measure object iteration performance might include: 1. **Array.prototype.forEach()**: Similar to `Object.keys()`, but applicable only to arrays. 2. **For-each loop syntax (for...of)**: Introduced in ECMAScript 2015, providing a concise way to iterate over objects using the `of` keyword. 3. **Native array methods**: Depending on the specific use case, other native array methods like `map()`, `filter()`, or `reduce()` might be used for iteration. Keep in mind that benchmarking JavaScript performance can be complex due to factors like caching, JIT optimizations, and varying browser implementations. Always consider multiple iterations, browsers, platforms, and hardware when conducting such tests.
Related benchmarks:
for-in vs object.keys Test2
for-in vs object.keys (2)
for in vs iterate over keys
for-in vs for object.keys keys
Comments
Confirm delete:
Do you really want to delete benchmark?