Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keybepb
(version: 0)
Comparing performance of:
for-in vs Object.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 };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { for (var key of Object.keys(obj)) { console.log(key); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.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):
I'll break down the provided benchmark for you. **Overview** The test case measures the performance difference between using traditional `for-in` loop and using the `Object.keys()` method to iterate over an object's properties in JavaScript. **What is being tested?** * The `for-in` loop: This is a traditional way of iterating over an object's properties. It uses a loop variable (`key`) that changes on each iteration, allowing access to both the property name and its value. * The `Object.keys()` method: This is a more modern approach to iterating over an object's properties. It returns an array of strings representing the property names, which can be used in a traditional loop. **Options being compared** The benchmark compares two approaches: 1. **Traditional `for-in` loop**: This uses a loop variable (`key`) that changes on each iteration. 2. **Using `Object.keys()` method**: This returns an array of strings representing the property names, which can be used in a traditional loop. **Pros and Cons** * **For-in loop** + Pros: Can access both property name and value, easier to use for iterating over arrays or objects with complex logic. + Cons: Can iterate over inherited properties (not recommended), slower performance due to the need to check each property's `in` flag. * **Object.keys() method** + Pros: Faster performance, more predictable results, does not include inherited properties. + Cons: Requires a traditional loop to access each property name, may not be suitable for arrays or objects with complex logic. **Library/Language features** None explicitly mentioned in the benchmark definition. **Special JS feature/syntax** The `for...of` loop (not used in this benchmark) and `const` (used in some test cases) are notable JavaScript features. However, they are not relevant to this specific benchmark. **Other alternatives** If you want to explore alternative approaches: * **Using `Array.prototype.forEach()`**: This is a more modern approach to iterating over arrays or objects. * **Using `for...of` loop**: This is a more concise way of iterating over arrays or objects, but not used in this benchmark. * **Using a traditional `while` loop with array indices**: This can be a good alternative for arrays, but may not be suitable for objects. **Benchmark preparation code** The script prepares an object (`obj`) with 11 properties, all initialized to the value `1`. The script then defines two test cases: * `for-in`: Uses a traditional `for-in` loop to iterate over the object's properties and logs each property name. * `Object.keys`: Uses the `Object.keys()` method to get an array of property names, which is then iterated using a traditional `for...loop`. **Latest benchmark result** The latest results show that: * The `for-in` loop performs slightly better than the `Object.keys()` method (approximately 9% faster) on this specific test case.
Related benchmarks:
for-in vs object.keys Test2
for-in vs object.keys (no console) (forked)
for-in vs object.keys vs object.values for objects - output object values
for-in vs object.keys vs object.values for objects 2.0
Comments
Confirm delete:
Do you really want to delete benchmark?