Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of Object.keys 2
(version: 6)
Comparing performance of:
for-in vs for-of Object.keys vs for-of Object.keys includes vs for-of Object.keys hasOwnProperty
Created:
5 years ago
by:
Registered User
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 (let i=10000; i > 0; i--) { for (let key in obj) { let x = key in obj; } }
for-of Object.keys
for (let i=10000; i > 0; i--) { let keys = Object.keys(obj); for (let key of keys) { let x = key in obj; } }
for-of Object.keys includes
for (let i=10000; i > 0; i--) { let keys = Object.keys(obj); for (let key of keys) { let x = keys.includes(key); } }
for-of Object.keys hasOwnProperty
for (let i=10000; i > 0; i--) { let keys = Object.keys(obj); for (let key of keys) { let x = Object.hasOwnProperty(obj, key); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for-in
for-of Object.keys
for-of Object.keys includes
for-of Object.keys hasOwnProperty
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 benchmark and its options. **Benchmark Overview** The benchmark measures the performance of three different approaches to iterate over an object's keys: 1. `for-in` loop 2. Using `Object.keys()` method with `for...of` loop 3. Using `includes()` method with `for...of` loop 4. Using `hasOwnProperty()` method with `for...of` loop **Options Compared** The benchmark compares the performance of these four approaches: * `for-in` loop: This is an older way of iterating over object properties using a loop. * `Object.keys()` method with `for...of` loop: This approach uses the `Object.keys()` method to get an array of keys, and then iterates over it using a `for...of` loop. * `includes()` method with `for...of` loop: This approach uses the `includes()` method to check if a key is present in the object, and then iterates over the object's keys using a `for...of` loop. * `hasOwnProperty()` method with `for...of` loop: This approach uses the `hasOwnProperty()` method to check if a property belongs to the object, and then iterates over the object's properties using a `for...of` loop. **Pros and Cons of Each Approach** 1. `for-in` loop: * Pros: Simple and widely supported. * Cons: Not as efficient as other methods, can iterate over inherited properties, and may not work well with modern JavaScript features. 2. `Object.keys()` method with `for...of` loop: * Pros: Efficient and modern approach. * Cons: Requires the `Object.keys()` method, which may not be supported in older browsers or environments. 3. `includes()` method with `for...of` loop: * Pros: Modern and efficient approach. * Cons: Not supported in older browsers or environments that don't have the `includes()` method. 4. `hasOwnProperty()` method with `for...of` loop: * Pros: Efficient and modern approach. * Cons: Requires the `hasOwnProperty()` method, which may not be supported in older browsers or environments. **Library Used** None of these approaches rely on a specific library. However, if we look at the `Object.keys()` method and its variants (e.g., `includes()`), they are built-in methods provided by modern JavaScript implementations. **Special JS Features/Syntax** This benchmark does not use any special or experimental JavaScript features, such as async/await, generators, or ES6+ modules. **Other Alternatives** If you're looking for alternative ways to iterate over object properties, you might consider: * Using a `for...in` loop with the `Object.getOwnPropertyNames()` method. * Using an array of keys and iterating over it using a traditional `for` loop. * Using a library like Lodash or Ramda, which provide functions for working with objects. Keep in mind that each approach has its own trade-offs, and the best choice depends 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?