var value = { a: 123 };
var valueNull = null;
var valueUndefined;
var zz;
function x() {
function y() {
function z() {
function f() {
function g() {
function h() {
function m() {
function n() {
function s() {
function t() {
function k() {
function q() {
function w() {
function o() {
function r() {
zz = {
callNull: function() {
value === null;
valueNull === null;
},
callUndefined: function() {
value === undefined;
valueUndefined === undefined;
},
callNotNull: function() {
value !== null;
valueNull !== null;
},
callNotUndefined: function() {
value !== undefined;
valueUndefined !== undefined;
}
};
} r();
} o();
} w();
} q();
} k();
} t();
} s();
} n();
} m();
} h();
} g();
} f();
} z();
} y();
} x();
function callNull() {
zz.callNull();
}
function callUndefined() {
zz.callUndefined();
}
function callNotNull() {
zz.callNotNull();
}
function callNotUndefined() {
zz.callNotUndefined();
}
callNull();
callUndefined();
callNotNull();
callNotUndefined();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
value === null | |
value === undefined | |
value !== null | |
value !== undefined |
Test name | Executions per second |
---|---|
value === null | 78873656.0 Ops/sec |
value === undefined | 77033040.0 Ops/sec |
value !== null | 76912592.0 Ops/sec |
value !== undefined | 73936736.0 Ops/sec |
The benchmark defined in the provided JSON is focused on measuring the performance of JavaScript expressions that perform comparisons against null
and undefined
. It specifically evaluates four scenarios:
Checking Equality with null
:
callNull();
value
is strictly equal to null
(value === null
), and also whether a variable valueNull
is strictly equal to null
.Checking Equality with undefined
:
callUndefined();
value
is strictly equal to undefined
(value === undefined
), and similarly checks if a variable valueUndefined
is strictly equal to undefined
.Checking Non-Equality with null
:
callNotNull();
value
is not equal to null
(value !== null
), and also whether valueNull
is not equal to null
.Checking Non-Equality with undefined
:
callNotUndefined();
value
is not equal to undefined
(value !== undefined
), and also checks if valueUndefined
is not equal to undefined
.The benchmark results indicate the number of executions per second for each of these comparisons. The results reveal:
null
(value === null
) is the fastest with approximately 78.87 million executions per second.undefined
follows closely at about 77.03 million executions per second.null
has about 76.91 million executions per second.undefined
is the slowest at 73.94 million executions per second.Equality Checks (===
):
Non-Equality Checks (!==
):
The benchmark doesn't utilize any external libraries; it is entirely based on native JavaScript. The test revolves around primitive operations to assess performance in comparison to JavaScript's handling of null
and undefined
.
Both null
and undefined
represent "no value," but they are used in different contexts:
null
is typically an assignment value, meaning "no value intentionally."undefined
represents a variable that has been declared but not assigned any value.Use of typeof
operator: Instead of direct comparisons, one can use typeof variable === "undefined"
for checks against undefined
. This approach is usually more descriptive but introduces overhead and is thus expected to be slower in performance benchmarks.
Logical Operators: In practice, sometimes the logical OR operator (||
) can be employed for default value assignments or checks, e.g., let val = providedValue || default;
, but such expressions vary from direct comparisons and are not directly covered in this benchmark.
Overall, this benchmark offers insights into fundamental comparative operations in JavaScript, highlighting performance aspects that might guide developers in making decisions based on execution speed versus clarity of intent.