Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys (2)
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
4 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
var keys = Object.keys(obj) for (var i=10000; i > 0; i--) { for(let i = 0; i < keys.length; i++) { console.log(keys[i]); } }
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
2.3 Ops/sec
Object.keys
2.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of two different approaches to iterate over an object's keys in JavaScript can be a fascinating benchmark. **What is being tested?** The provided JSON represents a microbenchmark that compares the performance of two methods: 1. `for-in` loop 2. Using the `Object.keys()` method Both methods are used to log each key-value pair of an object (`obj`) containing 10000 properties. **Options compared:** Two options are being compared in this benchmark: ### for-in Loop * **Approach:** The traditional JavaScript way of iterating over an object's properties using a `for-in` loop. * **How it works:** The `for-in` loop iterates over the object's own enumerable properties, and the variable `key` takes on the value of each property in turn. ### Object.keys() * **Approach:** Using the `Object.keys()` method to get an array of an object's own enumerable property names. * **How it works:** `Object.keys()` returns an array of strings representing the property names of the given object. The loop then iterates over this array using a `for` loop. **Pros and Cons:** ### for-in Loop Pros: * Well-supported in most browsers * Can be used with other data structures, like arrays Cons: * Performance can be slow due to the loop overhead * May not work as expected if the object has inherited properties or non-enumerable properties * Not suitable for large datasets, as it uses a linear search approach ### Object.keys() Pros: * Fast and efficient, especially when compared to `for-in` * Works well with modern JavaScript engines that optimize this method Cons: * May not work in older browsers that don't support the `Object.keys()` method * Requires an additional array lookup, which can be slower than a simple loop **Library:** In both cases, no library is explicitly mentioned. However, it's worth noting that some libraries, like Lodash or Underscore.js, may provide optimized versions of these methods. **Special JavaScript feature/syntax:** There are no special features or syntax used in this benchmark. The focus is solely on comparing the performance of two iteration approaches. **Other alternatives:** In addition to `for-in` and `Object.keys()`, there are other ways to iterate over an object's properties, such as: * Using a `forEach()` loop with a callback function * Using the `Array.prototype.forEach()` method (which can be used on an array of property names) * Using modern JavaScript features like `for...of` loops or `map()` However, these alternatives are not being tested in this specific benchmark. Overall, this microbenchmark provides valuable insight into the performance differences between two common iteration methods in JavaScript.
Related benchmarks:
For in vs For of
for-in vs Object.keys()
Object.keys(obj)[0] vs for in
for-in vs for object.keys keys
for-in vs object keys map vs object keys loop
Comments
Confirm delete:
Do you really want to delete benchmark?