<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var foo;
if (_.isUndefined(foo)) {
foo;
}
if (foo === undefined) {
foo;
}
if (!foo) {
foo;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.isUndefined | |
Native js | |
simplify native js |
Test name | Executions per second |
---|---|
lodash.isUndefined | 1024398848.0 Ops/sec |
Native js | 1030928384.0 Ops/sec |
simplify native js | 1036307200.0 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
The benchmark is comparing three different approaches to check if a variable foo
is undefined/unexistent:
isUndefined()
function=== undefined
!
operatorOptions Compared
We have three options being compared:
lodash.isUndefined()
function, which is a utility function from the Lodash library. It returns true
if the given value is undefined.=== undefined
to check if the variable foo
is undefined. The ===
operator checks for exact equality between two values, and in this case, it's being used to compare with the undefined
keyword.!
operator to negate the value of foo
. If foo
is falsey (i.e., it's null
, undefined
, 0, an empty string, etc.), this expression will evaluate to true. Otherwise, it will evaluate to false.Pros and Cons
===
operator can be confused with other comparisons, making it harder to read.!
).Library and Special JS Feature
The benchmark uses Lodash, which is a popular utility library for JavaScript. It provides various functions for tasks like string manipulation, array operations, and more.
There are no special JavaScript features or syntax being tested in this benchmark.
Alternatives
Other alternatives to check if a variable is undefined/unexistent include:
typeof
with the undefined
keyword: typeof foo === 'undefined'
||
operator with an empty string: `foo || ''in
operator with undefined
: foo in undefined
However, these alternatives are not tested in this benchmark.
Conclusion
In conclusion, this benchmark is testing three different approaches to check if a variable is undefined/unexistent: using Lodash's isUndefined()
function, native JavaScript's === undefined
operator, and simplifying the native approach with the !
operator. The pros and cons of each approach are discussed, highlighting the trade-offs between readability, performance, and simplicity.