Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Null and Empty Object check
(version: 1)
Different functions to test if a variable is null or empty object
Comparing performance of:
Object.keys vs Lodash isEmpty vs JSON
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.a = {}; window.b = null; window.c = undefined; window.d = { a: 1 }; window.e = { a: { b: 1 } };
Tests:
Object.keys
function func(val) { return val == null || (typeof val == 'object' && Object.keys(val) == 0) } func(window.a); func(window.b); func(window.c); func(window.d); func(window.e);
Lodash isEmpty
function func(val) { return _.isEmpty(val); } func(window.a); func(window.b); func(window.c); func(window.d); func(window.e);
JSON
function func(val) { return val === null || JSON.stringify(val) === '{}'; } func(window.a); func(window.b); func(window.c); func(window.d); func(window.e);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.keys
Lodash isEmpty
JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
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** The benchmark definition describes two functions that test if a variable is null or an empty object: 1. `func(val) { return val == null || (typeof val == 'object' && Object.keys(val) == 0) }` 2. `func(val) { return _.isEmpty(val); }` (using Lodash library) 3. `func(val) { return val === null || JSON.stringify(val) === '{}' }` These functions check for different conditions: * A variable being `null`. * An object with no keys (`Object.keys(val) == 0`). * An object that can be serialized to an empty string using `JSON.stringify()`. **Options Compared** The benchmark compares the performance of three different approaches: 1. **Native JavaScript**: Using the built-in `typeof` and `Object.keys` functions. 2. **Lodash library**: Using the `_.isEmpty` function from the Lodash library. 3. **JSON serialization**: Using the `JSON.stringify` function to check if an object can be serialized to an empty string. **Pros and Cons of Each Approach** 1. **Native JavaScript**: * Pros: Lightweight, no dependencies required. * Cons: May not work as expected for certain edge cases (e.g., NaN values). 2. **Lodash library**: * Pros: Robust and well-tested implementation, handles edge cases like NaN values. * Cons: Requires an additional dependency (Lodash), may introduce overhead due to function call. 3. **JSON serialization**: * Pros: Simple and efficient way to check for empty objects. * Cons: May not work as expected for certain types of objects (e.g., arrays, dates). **Library Used** The Lodash library is used in one of the benchmark test cases (`func(window.a); func(window.e);`). **Special JS Feature or Syntax** None mentioned. **Other Alternatives** In addition to the three approaches compared in the benchmark, other alternatives might include: * Using a custom implementation that combines elements of each approach. * Using other libraries (e.g., `lodash-es`) for testing purposes. * Optimizing the code for specific use cases or platforms.
Related benchmarks:
typeof undefined vs undefined equality check
typeof undefined vs undefined equality check vs double-equal
Object property: delete vs undefined 2
Lodash isEqual vs Every Undefined test
void 0 vs undefined vs variable containing undefined
Comments
Confirm delete:
Do you really want to delete benchmark?