<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
window.obj = {};
for (var i = 0, len = 100; i < len; i++) {
obj['key' + i] = 'value' + i;
}
_.isEmpty(window.obj);
const checkEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length;
checkEmpty(window.obj)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEmpty | |
https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_isempty |
Test name | Executions per second |
---|---|
_.isEmpty | 8604742.0 Ops/sec |
https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_isempty | 378953.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The benchmark is designed to compare two approaches for checking if an object is empty: Lodash's _.isEmpty()
function and a custom implementation using a library called "You-Dont-Need-Lodash-Underscore".
Lodash's _.isEmpty() Function
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array processing, and object manipulation.
The _isEmpty()
function takes an object as input and returns true
if the object has no properties (i.e., it is empty) and false
otherwise. The implementation is quite straightforward:
function _isEmpty(obj) {
return Object.keys(obj).length === 0;
}
This function uses the Object.keys()
method to get an array of the object's property names, and then checks if the length of that array is 0.
You-Dont-Need-Lodash-Underscore
The "You-Dont-Need-Lodash-Underscore" library (also known as YDNL) provides a subset of Lodash functions with a smaller footprint. The custom implementation for checking if an object is empty uses the following logic:
function isEmpty(obj) {
return Array.isArray(obj) || typeof obj === 'object' && Object.keys(obj).length === 0;
}
This implementation checks two conditions: (1) whether the object is an array, and (2) whether the object is a plain object with no properties. If either condition is true, it returns true
.
Pros and Cons
Here are some pros and cons of each approach:
_isEmpty()
function:Other Considerations
When choosing between these two approaches, consider the trade-offs between speed, simplicity, and library dependency. If you're working on a small project where performance is critical, the custom YDNL implementation might be a good choice. However, if you prioritize ease of use and wide support, Lodash's _isEmpty()
function is still a great option.
Other Alternatives
If you're not satisfied with either approach or need additional functionality for your empty object checks, consider these alternatives:
Object.keys()
method alone: This can be used to check if an object has any properties, but it requires iterating over the array of property names, which might be slower than using a specialized function like _isEmpty()
.Keep in mind that these alternatives may not offer the same level of performance, simplicity, or wide support as the original approaches.
I hope this explanation helps you understand the context behind the MeasureThat.net benchmark!