Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object emptyness big object
(version: 1)
test object emptyness with a big object
Comparing performance of:
Object.keys vs for in loop
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); } let abc = { a: 1, b: 2, c: 'a big string', d: { e: 123312, f: { g: 'big object' } } }
Tests:
Object.keys
Object.keys(abc).length === 0
for in loop
for (let prop in abc) { return abc.hasOwnProperty(prop) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys
for in loop
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys
74973112.0 Ops/sec
for in loop
65644712.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Benchmark Overview The benchmark focuses on evaluating the performance of two different methods for checking the emptiness of a large JavaScript object. The object (`abc`) has multiple properties, including nested objects and complex values. The two methods being compared are: 1. **Using `Object.keys()`** 2. **Using a `for...in` loop combined with `hasOwnProperty()`** ### Comparison of Methods #### Method 1: `Object.keys(abc).length === 0` - **Description**: This method creates an array of the object's own enumerable property names using `Object.keys()`, and checks if the length of that array is zero. If it is zero, the object is considered empty. - **Pros**: - Concise and straightforward to write. - Utilizes built-in JavaScript functions that are generally optimized for performance. - Clean and easier to read, especially for developers who may not be deeply familiar with JavaScript. - **Cons**: - If the object has a large number of properties, `Object.keys()` creates an intermediate array, which could add overhead. - Does not check for properties in the object's prototype chain. #### Method 2: `for (let prop in abc) { return abc.hasOwnProperty(prop) }` - **Description**: This approach uses a `for...in` loop to iterate through any enumerable properties of the object. It checks each property to see if it belongs to the object itself (not inherited) using the `hasOwnProperty()` method. - **Pros**: - Works for objects that may have additional inherited properties; it checks only the properties that belong to the object itself. - Directly iterates over properties without creating an intermediate array. - **Cons**: - The loop could introduce more complexity and is less efficient than `Object.keys()` for determining whether an object is empty since it will run until it finds the first property or completes the loop. - The return statement in the loop can be less intuitive for understanding the purpose of the code, especially for newcomers. ### Benchmark Results From the benchmark results, we have the following executions per second for each method on a Mac OS X environment using Chrome 131: 1. **Object.keys**: 74,973,112 executions per second 2. **for...in loop**: 65,644,712 executions per second ### Other Alternatives 1. **Using `JSON.stringify()`**: - One can convert the object to a JSON string and check if it equals `"{}"`. However, this approach typically has the overhead of serialization and might not be as performant as the methods above. ```javascript const isEmpty = (obj) => JSON.stringify(obj) === '{}'; ``` 2. **Using `Reflect.ownKeys()`**: - Similar to `Object.keys()`, but retrieves both enumerable and non-enumerable property names. You would then check the length to see if it's zero. ```javascript const isEmpty = (obj) => Reflect.ownKeys(obj).length === 0; ``` ### Conclusion When considering which method to use for checking object emptiness, developers should weigh the performance measured in the benchmarks, readability of the code, and specific use cases in their applications. While `Object.keys()` tends to be faster and easier to read, the `for...in` loop with `hasOwnProperty()` offers a solid alternative, especially when it's necessary to strictly ensure that no inherited properties are counted. Understanding these options helps in making informed decisions when optimizing JavaScript code.
Related benchmarks:
Anonymous Function in Loop
Anonymous Function in Loop
Anonymous Function in Loop
lodash isEmpty vs ES6
toString vs concatenation
weefwfe
function
Anonymous Function in Loop 2
Spread vs deconstruction
Comments
Confirm delete:
Do you really want to delete benchmark?