Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
is object with keys
(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_empty = {}; var obj_sm = {}; var obj_md = {}; var obj_lg = {}; var obj_xl = {}; for (let i=0; i<10000; i++) { if (i<10) obj_sm[Math.random()] = Math.random(); if (i<50) obj_md[Math.random()] = Math.random(); if (i<1000) obj_lg[Math.random()] = Math.random(); obj_xl[Math.random()] = Math.random(); } var isObject = val => !!val && typeof val === 'object' && val.constructor === Object;
Tests:
for in
const forIn = (o) => { if (!isObject(o)) return false; for (const k in o) if (o.hasOwnProperty(k)) return false; return true; } console.log(forIn(obj_empty)); console.log(forIn(obj_sm)); console.log(forIn(obj_md));
Object.keys
const keys = (o) => isObject(o) && Object.keys(o).length > 0; console.log(keys(obj_empty)); console.log(keys(obj_sm)); console.log(keys(obj_md));
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/122.0.0.0 Safari/537.36 Edg/122.0.0.0
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for in
34252.0 Ops/sec
Object.keys
34031.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, compared options, pros/cons, library usage, special JS features, and alternatives. **Benchmark Overview** The test is designed to measure the performance of two different approaches for checking if an object has keys: `for-in` loop and `Object.keys()` function. The benchmark creates several objects with varying levels of complexity (empty, small, medium, large) and checks each object using both approaches. **Script Preparation Code** The script prepares 5 objects (`obj_empty`, `obj_sm`, `obj_md`, `obj_lg`, `obj_xl`) by randomly assigning values to their keys. The script then defines the `isObject` function, which takes an object as input and returns `true` if it's a valid object. **Html Preparation Code** There is no HTML preparation code provided, so we'll focus on the JavaScript aspects of the benchmark. **Test Cases** The test cases are individual scripts that define two different functions: 1. `forIn`: A function that uses a `for-in` loop to check if an object has keys. It returns `false` for empty objects and iterates over each key in the object, checking if it exists using the `hasOwnProperty()` method. 2. `Object.keys`: A function that uses the `Object.keys()` method to get an array of an object's own enumerable property names. If the length of this array is greater than 0, it returns `true` for non-empty objects. **Library Usage** The `isObject` function uses the built-in JavaScript `typeof` operator and the `Object.constructor` property to determine if an object is a valid object. No external libraries are used in this benchmark. **Special JS Features** There's no special JavaScript feature or syntax used in these functions. They're simple, straightforward implementations of the two approaches being tested. **Comparison Options** The test compares the performance of: 1. `forIn`: A loop-based approach that iterates over each key in the object. 2. `Object.keys`: A function-based approach that uses the `Object.keys()` method to get an array of property names. **Pros and Cons of Each Approach** 1. **forIn**: * Pros: Simple, straightforward implementation. * Cons: May be slower due to loop overhead. * Note: The `hasOwnProperty()` check is necessary to avoid iterating over inherited properties. 2. **Object.keys**: * Pros: Faster and more concise than the `forIn` approach. * Cons: Requires a valid object with own enumerable properties. **Benchmark Results** The latest benchmark results show that: 1. `forIn`: 34251.99609375 executions per second (eps) on Chrome 122, Windows Desktop. 2. `Object.keys`: 34031.875 eps on Chrome 122, Windows Desktop. These results suggest that the `Object.keys` approach is slightly faster than the `forIn` loop-based approach in this specific benchmark. **Alternatives** Other approaches for checking if an object has keys include: 1. Using `Object.getOwnPropertyNames()` or `Object.getOwnPropertySymbols()` to get an array of all property names, including inherited ones. 2. Implementing a custom function using recursion or bit manipulation. 3. Using libraries like Lodash's `_.keys()` method. However, the original `forIn` and `Object.keys()` approaches are still widely used and well-established solutions for this problem.
Related benchmarks:
Map vs Array vs Object has uint32 key speed
Object keys vs Array map v2
keys vs values
Object.keys(object).includes(key) vs key in object
Object.entries VS Object.keys with obj[key]
Comments
Confirm delete:
Do you really want to delete benchmark?