Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Nullish coalescing vs logical OR operators
(version: 0)
Comparing performance of:
Nullish coalescing vs Logical OR
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var nullValue = null; var undefinedValue; var trueValue = true;
Tests:
Nullish coalescing
nullValue ?? undefinedValue ?? trueValue
Logical OR
nullValue || undefinedValue || trueValue
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Nullish coalescing
Logical OR
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 OPR/120.0.0.0 (Edition Yx 05)
Browser/OS:
Opera 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Nullish coalescing
230605840.0 Ops/sec
Logical OR
244837184.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance difference between two different approaches to handle `null` or `undefined` values: 1. **Nullish coalescing**: This syntax, introduced in ECMAScript 2020, allows for a more concise way to specify a default value when dealing with `null` or `undefined`. It's denoted by three consecutive question marks (`??`) followed by the desired default value. 2. **Logical OR operators**: This approach uses the pipe (`|`) operator, which is commonly used in programming languages like C and Python. **Options comparison** The benchmark compares these two approaches to see which one is faster: Pros of Nullish coalescing: * More concise syntax * Can be more readable, especially for developers familiar with this syntax Cons of Nullish coalescing: * May not be supported in older browsers or environments * Requires ECMAScript 2020 support Pros of Logical OR operators: * Widely supported across different browsers and environments * Easy to understand and implement, even for developers new to the concept Cons of Logical OR operators: * More verbose syntax compared to Nullish coalescing **Library and special JavaScript feature** There are no libraries mentioned in this benchmark. However, note that ECMAScript 2020 features like nullish coalescing were introduced as a standard, which means they're now widely supported across modern browsers. **Special JS feature or syntax** The benchmark uses the new nullish coalescing syntax (`??`). If you're not familiar with this syntax, it's similar to the ternary operator in C-like languages: ```javascript x ?? y; // equivalent to x || (x === undefined ? y : x) ``` This syntax is supported in modern JavaScript engines and browsers. **Alternatives** Other approaches to handling `null` or `undefined` values include: * Using a separate function to handle null/undefined values: `const value = getValue(); const result = value !== undefined ? value : defaultValue;` * Using a conditional statement: `if (value === null || value === undefined) { return defaultValue; } else { return value; }` * Using the optional chaining operator (`?.`): `const result = object?.prop ?? defaultValue;` These alternatives are more verbose and may not be as efficient as Nullish coalescing, but they're still valid ways to handle null/undefined values in JavaScript.
Related benchmarks:
Nullish coalescing (??) vs logical OR (||)
Nullish coalescing V.S. Logic or operator (native)
if(!variable) vs if(variable===undefined) performance
Nullish coalescing, logical OR
Comments
Confirm delete:
Do you really want to delete benchmark?