Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys vs for in loop
(version: 0)
Comparing performance of:
for in loop vs Object.keys
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = {}; for (var i = 0; i < 10000; i++) { data[`a${i}`] = i; }
Tests:
for in loop
let hasKey = false; for (let key in data) { if (key === 'a998') { hasKey = true; } }
Object.keys
const hasKey = Object.keys(data).includes('a998');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for in loop
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 YaBrowser/24.7.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for in loop
1622.8 Ops/sec
Object.keys
1926.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition JSON:** The benchmark is comparing two approaches to check if a key exists in an object: 1. **For-in loop**: Using a traditional for-in loop to iterate over the object's properties and check if one of them matches the desired key. 2. **Object.keys() method**: Utilizing the `Object.keys()` method to get an array of the object's property names, and then using the `includes()` method to check if the desired key is present. **Options Compared:** * For-in loop (traditional iteration) * Object.keys() method (modern approach using arrays) **Pros and Cons:** * **For-in loop:** + Pros: - Portable across older browsers that don't support `Object.keys()`. - Allows for easy extension with additional logic in the callback function. + Cons: - Can be slower due to the overhead of iterating over the object's properties. - May not work as expected if the object has inherited properties. * **Object.keys() method:** + Pros: - Faster, since it uses an optimized internal implementation. - More readable and concise code. + Cons: - May not work in older browsers that don't support `Object.keys()`. **Library and Purpose:** In the benchmark definition JSON, the "data" object is initialized using a for loop. This suggests that this script is intended to be run on multiple iterations, with new data being generated each time. The purpose of this setup is likely to test the performance of both approaches under different conditions. There are no external libraries mentioned in the provided code snippets. However, it's worth noting that the "data" object and its properties might rely on some internal library or feature, but they do not explicitly use any well-known JavaScript libraries like jQuery, Lodash, etc. **Special JS Feature/Syntax:** None of the provided benchmark definitions use any special JavaScript features or syntax. They are relatively simple examples aimed at showcasing the performance difference between two common approaches to checking if a key exists in an object. **Alternative Approaches:** Other ways to check if a key exists in an object include: * Using the `in` operator directly (e.g., `"a998" in data`) * Utilizing the `hasOwnProperty()` method (e.g., `data.hasOwnProperty('a998')`) * Creating a custom function or module to perform this operation However, these alternatives are not being tested by MeasureThat.net, as they were not included in the provided benchmark definition.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.entries vs Object.keys vs for...in
Object entries vs forin
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?