-
-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor to simplify null
or undefined
checks
#471
base: main
Are you sure you want to change the base?
Refactor to simplify null
or undefined
checks
#471
Conversation
I think for Valibot I prefer an explicit check for both values. Also, as far as I know, |
You are absolutely correct, it should indeed align with your preference, and there truly isn't a right or wrong choice here. I believe there is not much difference between the two methods in terms of performance and bundle size. I merely noticed this approach being frequently used in various libraries, which is why I attempted to initiate this PR. Thank you very much for your response. |
Thank you for your response! I will keep this PR open for now. I will look into it when I start working on the other library I mentioned. |
For reference, I have included a simple code snippet to compare execution speeds. In my environment, while there was hardly any difference observed when running on Node, on Bun, the '===' operation was found to be 2.8 times slower. function useEqualityOp(x) {
return x == null;
}
function useStrictEqualityOp(x) {
return x === null && x === undefined;
}
// Test data
const testData = [null, undefined, 0, false, "", {}, []];
const iterations = 10000000;
// Measure the execution time of ==
const start1 = performance.now();
for (let i = 0; i < iterations; i++) {
for (let j = 0; j < testData.length; j++) {
useEqualityOp(testData[j]);
}
}
const end1 = performance.now();
console.log("==:", end1 - start1, "milliseconds");
// Measure the execution time of ===
const start2 = performance.now();
for (let i = 0; i < iterations; i++) {
for (let j = 0; j < testData.length; j++) {
useStrictEqualityOp(testData[j]);
}
}
const end2 = performance.now();
console.log("===:", end2 - start2, "milliseconds"); Node v20.11.1:
Bun v1.0.26:
|
Thank you for your research! |
Currently, the methods
nonNullish
,nonNullishAsync
,nullish
, andnullishAsync
determine ifinput
isnull
orundefined
using the following approach:According to the ECMAScript specification mentioned in:
13.11.1 Runtime Semantics: Evaluation
EqualityExpression : EqualityExpression == RelationalExpression
The specification for IsLooselyEqual is as follows:
7.2.14 IsLooselyEqual ( x, y )
The abstract operation IsLooselyEqual takes arguments x (an ECMAScript language value) and y (an ECMAScript language value) and returns either a normal completion containing a Boolean or a throw completion. It provides the semantics for the == operator. It performs the following steps when called:
a. Return IsStrictlyEqual(x, y).
...ellipsis
In points two and three, we find that using
null == undefined
yieldstrue
. Therefore, we can simplify our check as follows:It perhaps makes the code more concise. If there are any concerns, we are eager to hear your thoughts.