Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.isEmpty vs Object.keys.length vs custom isEmpty method (10)
(version: 1)
Comparing performance of:
_.isEmpty vs Object.keys().length vs isEmpty
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Script Preparation code:
window.obj = {}; for (var i = 0, len = 10; i < len; i++) { obj['key' + i] = 'value' + i; } function isEmpty(obj) { for (const prop in obj) { if (Object.hasOwn(obj, prop)) { return false; } } return true; }
Tests:
_.isEmpty
_.isEmpty(window.obj);
Object.keys().length
Object.keys(window.obj).length === 0;
isEmpty
isEmpty(window.obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.isEmpty
Object.keys().length
isEmpty
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
_.isEmpty
24992754.0 Ops/sec
Object.keys().length
85288888.0 Ops/sec
isEmpty
73887056.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON evaluates the performance of three different methods for checking if an object in JavaScript is empty: 1. **_.isEmpty from Lodash Library** 2. **Object.keys().length === 0** 3. **A custom isEmpty function implemented within the benchmark** ### Library Involved: Lodash The `_.isEmpty` method comes from the Lodash library, which is a popular JavaScript utility library that provides various functions for common programming tasks. The primary purpose of `_.isEmpty` is to determine if a collection (array, object, or string) is empty. In this benchmark, its performance is compared to other native JavaScript approaches for checking if an object is empty. ### Test Cases Explained - **`_.isEmpty(window.obj);`**: This test uses the `_.isEmpty` function. Lodash internally uses key retrieval and checks the size of the object to determine emptiness. This method is convenient but may incur some overhead due to the function call and additional checks. - **`Object.keys(window.obj).length === 0;`**: This second test leverages the `Object.keys` method, which returns an array of a given object's own enumerable property names. By checking the length of this array, it can reliably determine if the object has no properties. This is a direct approach that uses native JavaScript, making it quick and efficient. - **`isEmpty(window.obj);`**: This test utilizes a custom function named `isEmpty`, which iterates over the object's properties with a `for..in` loop and checks if any properties are present using the `Object.hasOwn` method. While this function is also native, it may have more overhead due to the iteration, especially for larger objects, but it provides greater control and customization over the checking mechanism. ### Pros and Cons of Each Approach 1. **_.isEmpty (Lodash)** - **Pros**: - Highly convenient and readable. - Handles various data types uniformly. - **Cons**: - Potentially slower due to the overhead of loading the library and the function call. - Increases bundle size if Lodash is used in a project without need for other functions. 2. **Object.keys().length === 0** - **Pros**: - Fast and efficient as it directly uses native functions. - No additional dependencies or library overhead. - **Cons**: - Less readable for those unfamiliar with JavaScript standard methods. 3. **Custom isEmpty function** - **Pros**: - Customizable and controllable; developers can expand functionality if required. - **Cons**: - Potentially less efficient due to iteration. - More code to maintain and verify correctness. ### Alternative Methods While the three methods evaluated are effective, there are alternative ways to check if an object is empty in JavaScript: - **Using `Object.entries`**: Check if the length of `Object.entries(obj)` equals zero: `Object.entries(obj).length === 0;` - **Using `JSON.stringify`**: A more unconventional method could involve checking if `JSON.stringify(obj)` results in an empty object: `JSON.stringify(obj) === '{}'`. Though this approach is less performant and unnecessary for this task. ### Conclusion In choosing between these options, developers should consider the context in which they are working, including performance requirements, dependency management, readability, and maintainability. For simple cases, the native `Object.keys().length` method may be sufficient and optimal, while `_.isEmpty` may appeal in scenarios where code readability and utility library usage is prioritized.
Related benchmarks:
_.isEmpty vs Object.keys.length 1
for-in vs object.keys isEmpty
_.isEmpty vs for-in
isEmpty tests
_.isEmpty vs Object.keys.length vs for in
_.isEqual vs Object.is
Check If Object Empty 2
_.isEmpty vs Object.keys.length vs custom isEmpty method (10) alt is empty
_.isEmpty vs Object.keys.length vs custom isEmpty method (10000) alt is empty
Comments
Confirm delete:
Do you really want to delete benchmark?