Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys4112
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { item1: { isTexture: true }, item2: { isTexture: false }, item3: { isTexture: true }, item4: { isTexture: false }, item5: { isTexture: true }, item6: { isTexture: false }, item7: { isTexture: true }, item8: { isTexture: true }, item9: { isTexture: false }, item10: { isTexture: true }, item11: { isTexture: false }, item12: { isTexture: true }, item13: { isTexture: false }, item14: { isTexture: true }, item11: { isTexture: true }, item21: { isTexture: false }, item31: { isTexture: true }, item41: { isTexture: false }, item51: { isTexture: true }, item61: { isTexture: false }, item71: { isTexture: true }, item81: { isTexture: true }, item91: { isTexture: false }, item101: { isTexture: true }, item111: { isTexture: false }, item121: { isTexture: true }, item131: { isTexture: false }, item141: { isTexture: true } };
Tests:
for-in
for (var i=10000; i > 0; i--) { let texturesForIn = []; for (const key in obj) { const value = obj[key]; if (value && value.isTexture) { texturesForIn.push(value); } } }
Object.keys
for (var i=10000; i > 0; i--) { let texturesObjectKeys = []; const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = obj[key]; if (value && value.isTexture) { texturesObjectKeys.push(value); } } }
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
75.2 Ops/sec
Object.keys
70.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark is comparing two approaches: `for-in` loop and using `Object.keys()` method to iterate over an object's properties. **Script Preparation Code** The script preparation code defines a JavaScript object `obj` with 14 properties, each containing an `isTexture` property that can be either true or false. The goal of the benchmark is to count the number of objects in `obj` for which `isTexture` is true using both approaches. **Html Preparation Code** There is no HTML preparation code provided, so we'll assume that it's not relevant to this specific benchmark. **Individual Test Cases** The benchmark consists of two test cases: 1. **for-in**: This test case uses a traditional `for-in` loop to iterate over the object's properties. The script counts the number of objects with `isTexture` set to true and stores the result in an array called `texturesForIn`. 2. **Object.keys()**: This test case uses the `Object.keys()` method to get an array of the object's property names, which is then used as a loop counter. The script counts the number of objects with `isTexture` set to true and stores the result in an array called `texturesObjectKeys`. **Library and Special Features** Neither of these test cases uses any external libraries or special JavaScript features beyond standard ECMAScript 2022. **Pros and Cons of Each Approach** 1. **for-in**: This approach is generally faster because it avoids the overhead of creating a new array with property names. * Pros: Can be slightly faster, more readable for simple loops. * Cons: May have issues with object literals or cyclic references, can be slower if the loop doesn't terminate early (e.g., due to using `in` operator). 2. **Object.keys()**: This approach is generally slower because it involves creating a new array and iterating over its length. * Pros: More concise and readable, avoids issues with object literals or cyclic references. * Cons: Can be slower, may use more memory. **Other Considerations** * The benchmark seems to focus on the performance difference between these two approaches. However, other factors like code readability, maintainability, and potential issues with specific JavaScript environments could also be relevant when choosing an approach. * It's worth noting that `Object.keys()` has been a part of ECMAScript since 2009, so this benchmark is testing the performance of a relatively mature feature. **Alternatives** Other alternatives to these approaches include: 1. Using a `for...of` loop with `Object.entries()`, which would avoid creating an array of property names. 2. Utilizing a library like Lodash or Ramda for iteration and filtering, which might provide additional benefits (e.g., handling cyclic references) but could also introduce performance overhead. Keep in mind that the best approach depends on the specific use case and requirements.
Related benchmarks:
Test Immutable ToJS vs get
Test Immutable ToJS vs single get
for-in vs object.keys411
for-in vs object.keys vs object.values for objects 2.0
Comments
Confirm delete:
Do you really want to delete benchmark?