Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for object.keys
(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
for (var i=10000; i > 0; i--) { const keys = Object.keys(obj) 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:
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 explain what's tested in the provided JSON benchmark. The test case compares two ways to iterate over an object: using `for...in` and using `Object.keys()` with a `for...of` loop. **For-in Loop** The first benchmark uses a traditional `for...in` loop to iterate over the object's properties. This loop iterates over the object's property names (i.e., strings like `'a'`, `'b'`, etc.) and executes the code inside the loop for each property found in the object. Pros: * Easy to read and write, as it mirrors how we typically access properties on an object. * Works with objects that have inherited properties (not just own properties). Cons: * Can iterate over inherited properties, which may not be what you want if you only care about the object's own properties. * Can also iterate over symbols in modern JavaScript engines. **Object.keys() with for...of Loop** The second benchmark uses `Object.keys()` to get an array of property names from the object and then iterates over that array using a `for...of` loop. This approach is more explicit and avoids any potential issues with inherited properties or symbols. Pros: * More controlled iteration, as it only considers own properties (and does not iterate over inherited ones). * Generally faster and more efficient than the `for-in` loop. Cons: * May be less readable for those familiar with `for...in`, as it requires a bit more explicit code. In this specific benchmark, both approaches are iterating over an object with 7 properties. The results show that the approach using `Object.keys()` with a `for...of` loop is faster than the `for-in` loop. **Library and Special JavaScript Features** There's no library used in these benchmarks. However, it's worth noting that some browsers may use optimized implementations of `Object.keys()` under the hood, which could affect the results. One special JavaScript feature mentioned here is the `RawUAString`, which is a string containing information about the browser and its environment (in this case, Chrome 95 on Windows). This string can be useful for analyzing the benchmarking data. **Alternatives** If you want to compare other iteration methods over objects, some alternatives include: * Using `for...of` with a custom iterator function (more flexible than using `Object.keys()`). * Using `forEach()` (a more modern and concise way to iterate over arrays of property names). * Using `Reflect.ownKeys()` (another method for getting an array of own property names, which is supported in ES6+). Keep in mind that each approach has its pros and cons, and the best choice will depend on your specific use case and requirements.
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?