Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys test TBM
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
2 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) { var x = key; } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(obj).forEach(key => {var x = 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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
3715.9 Ops/sec
Object.keys
1412.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's being tested in this JavaScript benchmark. **Benchmark Overview** The benchmark measures the performance of two approaches for iterating over an object's properties: `for-in` and `Object.keys`. The test creates an object with 11 properties, each initialized to 1. The benchmark script then iterates over this object using both approaches, printing the value of a variable `x` assigned to each property key. **Options Compared** The two options being compared are: 1. **`for-in`**: This is a traditional loop construct in JavaScript that allows iteration over an object's properties using the property name as a variable. 2. **`Object.keys()`**: This method returns an array of property names for an object, which can be iterated over using a `forEach` loop. **Pros and Cons** * **`for-in`**: + Pros: Can be more efficient in some cases, especially when iterating over objects with many properties. + Cons: May not provide the expected results if the property names are non-string or have certain values (e.g., undefined, null). * **`Object.keys()`**: + Pros: Provides a consistent and predictable way to iterate over object properties, regardless of their type or value. + Cons: May be less efficient than `for-in` for large objects due to the overhead of creating an array. **Library** None. The benchmark uses native JavaScript features only. **Special JS Feature or Syntax** No special syntax or features are used in this benchmark. However, it's worth noting that the `for-in` loop is sensitive to property names with certain values (e.g., undefined, null), which may affect its performance and accuracy. **Other Alternatives** If you're interested in optimizing JavaScript performance, here are some alternative approaches you might consider: * **`Array.prototype.forEach()`**: This method provides a similar way to iterate over an array or object's properties as `Object.keys()`. * **`for...of` loop**: Introduced in ECMAScript 2015, this loop construct allows iteration over iterable objects (e.g., arrays, sets) and provides a more concise syntax than traditional loops. * **`Map` data structure**: A JavaScript implementation of the Map data structure can provide faster lookups and iteration over its keys. Keep in mind that each approach has its trade-offs and may be suitable for different use cases. The choice ultimately depends on your specific requirements, performance constraints, and personal preference.
Related benchmarks:
for-in vs object.keys vs object.values for objects v2
for-in vs object.keys vs object.values for objects v3
for-in vs object.keys vs object.values for objects test
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?