<!--your preparation HTML code goes here-->
var stringArr = Array(10_000).fill(undefined).map((_, index) => `${index + 1}`);
var nullishArr = Array(10_000).fill(undefined).map(() => null);
const stringResult = stringArr.filter((v) => typeof v === 'string');
const nullishResult = nullishArr.filter((v) => typeof v === 'string');
const stringResult = stringArr.filter((v) => v != null);
const nullishResult = nullishArr.filter((v) => v != null);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
typeof string | |
nullish check |
Test name | Executions per second |
---|---|
typeof string | 12941.8 Ops/sec |
nullish check | 8637.6 Ops/sec |
The benchmark titled "typeof string vs nullish check" is designed to compare the performance of two different techniques for checking values in JavaScript arrays. This benchmark utilizes two arrays: stringArr
, which contains string representations of numbers, and nullishArr
, which is filled with null
values.
Test Case: typeof string
const stringResult = stringArr.filter((v) => typeof v === 'string');
const nullishResult = nullishArr.filter((v) => typeof v === 'string');
string
. The typeof
operator is employed here, which returns a string indicating the type of the unevaluated operand.typeof
might incur a performance penalty due to the overhead of type checking for each element.Test Case: nullish check
const stringResult = stringArr.filter((v) => v != null);
const nullishResult = nullishArr.filter((v) => v != null);
null
(or undefined
due to the use of loose inequality). The use of !=
checks both null
and undefined
values, effectively filtering out undefined values in the stringArr
and keeping null
in the nullishArr
.null
and undefined
) and can be more performant than checking types if a large volume of undefined is expected.!=
, it handles both null and undefined, which is often desirable in JavaScript's loosely typed nature.0
or ""
) being considered valid.typeof
, which could lead to ambiguity in certain scenarios.The results show the execution speed in terms of "Executions Per Second" for each test case on Chrome 131 running on macOS:
typeof string
: ~19,467 executions per second.nullish check
: ~18,519 executions per second.From the results:
typeof
check appears to be slightly faster than the nullish check on this particular setup.When considering approaches for similar benchmarking:
Alternatives to typeof
and nullish checks:
Array.prototype.every()
could potentially be a way to validate all elements of an array if you are checking for integrity and require a boolean outcome rather than filtering. This adds overhead if returning specific values.Libraries: No specific external libraries are used in this benchmark. However, when working with more complex data structures, libraries like Lodash (for utility functions) could be beneficial, offering optimized functions for deep checks or robust filtering capabilities.
Understanding the distinction and performance implications of these approaches allows developers to make informed decisions when implementing data type validation and manipulation in their applications.