Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isNullish variants 5
(version: 0)
Comparing performance of:
nullish coalescing vs === null vs === undefined
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) { arr[i] = i % 3 === 0 ? i : i % 3 === 1 ? null : undefined i++; }
Tests:
nullish coalescing
const isNullish = (value) => (value ?? true) === true; arr.forEach(i => isNullish(i));
=== null
const isNullish = value => value === null; arr.forEach(i => isNullish(i));
=== undefined
const isNullish = value => value === undefined; arr.forEach(i => isNullish(i));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
nullish coalescing
=== null
=== undefined
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's tested in the provided JSON, describe the options compared, and their pros and cons. **Benchmark Test** The test measures performance of JavaScript code that checks if a value is nullish (i.e., either null or undefined) using different approaches. The benchmark compares three variants: 1. `=== null`: This option uses the strict equality operator (`===`) to check if a value is null. 2. Nullish Coalescing Operator (`??`): This option uses the coalescing operator, which returns the first operand if it's not null or undefined, and the second operand otherwise. 3. Comparison with `undefined`: This option directly compares a value with `undefined`. **Library: None** There are no external libraries used in these benchmark tests. **Special JS Features/Syntax: Nullish Coalescing Operator (`??`)** The test uses the nullish coalescing operator, which is a relatively new feature introduced in ECMAScript 2020. This operator allows you to use a single expression to provide a default value when a variable or property might be null or undefined. **Options Compared** Here's a brief description of each option: 1. `=== null`: * Pros: Simple and efficient. * Cons: May not work as expected for all types of values (e.g., NaN, symbols). 2. Nullish Coalescing Operator (`??`): * Pros: More readable and maintainable, handles various edge cases. * Cons: Newer feature, may have performance implications due to its complexity. 3. Comparison with `undefined`: * Pros: Simple and easy to understand. * Cons: May be slower than the other two options, as it involves a direct comparison. **Benchmark Preparation Code** The preparation code generates an array of 100,000 elements using the script provided in the benchmark definition. Each element is either assigned a value that's a multiple of 3, or set to null or undefined using the script's logic. **Other Alternatives** If you were to implement this test yourself, alternative approaches could include: * Using a switch statement instead of an array * Utilizing other conditional statements like `if`/`else if`/`else` * Employing a different data structure, such as a Set or Map Keep in mind that the chosen approach should balance performance, readability, and maintainability.
Related benchmarks:
Flatten arrays
isNullish variants 4
isNullish variants 3
var is a non-empty array (v2)
Comments
Confirm delete:
Do you really want to delete benchmark?