Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys3
(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 (let i=10000; i > 0; i--) { for (let key in obj) { const x = i; } }
Object.keys
for (let i=10000; i > 0; i--) { Object.keys(obj).forEach(key => {const x = 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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
2882.6 Ops/sec
Object.keys
909.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The benchmark definition specifies two different approaches for iterating over an object: 1. `for-in`: This approach uses the `for...in` loop, which iterates over the property names of an object. 2. `Object.keys()`: This approach uses the `Object.keys()` method, which returns an array of the object's own enumerable property names. **Options Compared** The two options being compared are: * Using `for-in` to iterate over the object's properties * Using `Object.keys()` to get an array of property names and then iterating over it using `forEach()` **Pros and Cons** * **for-in**: + Pros: Can be more concise and efficient for simple iterations. + Cons: May have performance issues if the object is large or has many properties, as it has to iterate over all properties even if they're not needed. Additionally, it can be slower than `Object.keys()` due to its proprietary nature. * **Object.keys()**: + Pros: More predictable and reliable performance, as it uses a standardized API. It's also more modern and widely supported. + Cons: May be less concise or efficient for simple iterations. **Other Considerations** The benchmark definition doesn't include any special JS features or syntax, so we'll focus on the general options being compared. **Library Use** In this benchmark, the `Object.keys()` method is used from the standard JavaScript library. There's no external library mentioned. Now, let's take a look at the individual test cases: * **Test Case 1: "for-in"** + The benchmark definition uses a simple `for...in` loop to iterate over the object's properties. + This is a basic example that demonstrates how to use `for-in` for iteration. * **Test Case 2: "Object.keys()"** + The benchmark definition uses `Object.keys()` to get an array of property names and then iterates over it using `forEach()`. + This shows how to use `Object.keys()` in a real-world scenario. **Other Alternatives** If you're looking for alternative approaches, you could consider: * Using `for...of` loops instead of `for-in`, which is more modern and efficient. * Using `Array.prototype.forEach.call()` or other methods that iterate over arrays and objects in different ways. * Implementing your own custom iteration logic using `Object.keys()`, `Object.values()`, or `Object.entries()`. Keep in mind that the choice of approach depends on the specific use case, performance requirements, 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?