let x = null ?? undefined ?? null ?? undefined ?? null ?? undefined ?? null ?? undefined ?? null ?? undefined ?? 1;
let x = null || undefined || null || undefined || null || undefined || null || undefined || null || undefined || 1;
let x = 1 ?? undefined ?? null ?? undefined ?? null ?? undefined ?? null ?? undefined ?? null ?? undefined ?? 1;
let x = 1 || undefined || null || undefined || null || undefined || null || undefined || null || undefined || 1;
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
let x = (_j = (_h = (_g = (_f = (_e = (_d = (_c = (_b = (_a = null !== null && null !== void 0 ? null : undefined) !== null && _a !== void 0 ? _a : null) !== null && _b !== void 0 ? _b : undefined) !== null && _c !== void 0 ? _c : null) !== null && _d !== void 0 ? _d : undefined) !== null && _e !== void 0 ? _e : null) !== null && _f !== void 0 ? _f : undefined) !== null && _g !== void 0 ? _g : null) !== null && _h !== void 0 ? _h : undefined) !== null && _j !== void 0 ? _j : 1;
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
let x = (_j = (_h = (_g = (_f = (_e = (_d = (_c = (_b = (_a = 1 !== null && 1 !== void 0 ? 1 : undefined) !== null && _a !== void 0 ? _a : null) !== null && _b !== void 0 ? _b : undefined) !== null && _c !== void 0 ? _c : null) !== null && _d !== void 0 ? _d : undefined) !== null && _e !== void 0 ? _e : null) !== null && _f !== void 0 ? _f : undefined) !== null && _g !== void 0 ? _g : null) !== null && _h !== void 0 ? _h : undefined) !== null && _j !== void 0 ? _j : 1;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Nullish coalescing (??) | |
Logical OR (||) | |
Nullish coalescing (??) [short-circuit] | |
Logical OR (||) [short-circuit] | |
JS Compiled ?? (?:) | |
JS Compiled ?? (?:) [short-circuit] |
Test name | Executions per second |
---|---|
Nullish coalescing (??) | 84215360.0 Ops/sec |
Logical OR (||) | 83704592.0 Ops/sec |
Nullish coalescing (??) [short-circuit] | 107816688.0 Ops/sec |
Logical OR (||) [short-circuit] | 106414608.0 Ops/sec |
JS Compiled ?? (?:) | 76080736.0 Ops/sec |
JS Compiled ?? (?:) [short-circuit] | 107635392.0 Ops/sec |
The benchmark you're analyzing compares three different JavaScript constructs related to handling nullish values, specifically using:
Nullish Coalescing (??)
let x = null ?? undefined ?? ...
let x = 1 ?? undefined ?? ... [short-circuit]
null
or undefined
. If both are non-nullish values, it returns the left operand.0
or an empty string and values that are null
or undefined
.Logical OR (||)
let x = null || undefined || ...
let x = 1 || undefined || ... [short-circuit]
true
; otherwise, it returns the right operand.0
, false
, ""
) the same as null
or undefined
, which may lead to unintended behavior if you only want to check for nullish values.JS Compiled Conditional (?:) Operator
let x = (_j = (_h = ...) !== null && _a !== void 0 ? _a : ...
let x = (_j = (_h = ...) !== null && _b !== void 0 ? _b : ... [short-circuit]
Other alternatives for handling default values include:
lodash
or underscore.js
, which provide methods such as _.get
or _.defaultTo
, which may encapsulate this type of logic.In summary, this benchmark provides insights into the performance and semantics of different approaches to handling null-like values in JavaScript, emphasizing important distinctions in how developers can manage defaults accurately and efficiently.