Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
|| vs ?? operation
(version: 0)
Comparing performance of:
|| vs ??
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
||
const a = 3 || 0 const b = 0 || 0 const c = '1212' || 0 const d = undefined || 0 const e = null || 0 const f = {} || 0 const g = '' || 0 const h = [] || 0
??
const a = 3 ?? 0 const b = 0 ?? 0 const c = '1212' ?? 0 const d = undefined ?? 0 const e = null ?? 0 const f = {} ?? 0 const g = '' ?? 0 const h = [] ?? 0
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
||
??
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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two conditional operator approaches: the OR (`||`) operator and the nullish coalescing (`??`) operator. Both operators are used to provide a default value when the operand is falsy (e.g., `0`, `undefined`, `null`, `false`, `''`, etc.). **Options Compared** The benchmark compares two options: 1. **OR (`||`) Operator**: This operator returns the first "truthy" value it encounters, or the second operand if both are falsy. 2. **Nullish Coalescing (`??`) Operator**: This operator returns the first operand if it's truthy, otherwise returns the second operand. **Pros and Cons of Each Approach** **OR (`||`) Operator:** Pros: * Simple and widely supported in JavaScript * Can be used to provide a default value when an expression is falsy Cons: * Requires two operands (a truthy value and a default value) * May not always produce the desired behavior, especially with object comparisons (e.g., `x || y` might return the reference to `y` instead of its actual value) **Nullish Coalescing (`??`) Operator:** Pros: * More readable and expressive than `||`, as it explicitly conveys the intent to provide a default value when an operand is falsy * Less prone to unexpected behavior with object comparisons Cons: * Newer than `||` and may not be supported in all browsers or environments (although this benchmark suggests Chrome 116 supports it) * May have slightly slower performance compared to `||` **Library Usage** None of the test cases use any external libraries. **Special JS Features or Syntax** The benchmark uses the nullish coalescing operator (`??`) and the OR operator (`||`). Both are standard JavaScript operators, but their usage is not as common as other operators like `+`, `-`, `*`, etc. The benchmark highlights the differences between these two operators in providing default values. **Other Alternatives** If you need to provide a default value when an expression is falsy, besides `||` and `??`, you can also use: 1. **Default parameters**: Assign a default value directly to a function parameter. 2. **Optional chaining**: Use the optional chaining operator (`?.`) to access properties with null or undefined values. In summary, the benchmark compares two conditional operator approaches: OR (`||`) and nullish coalescing (`??`). While both can be used to provide default values, `nullish coalescing` is a more expressive and readable way to achieve this, but may require support in all browsers or environments.
Related benchmarks:
Which operator is faster for indexOf ( '>' vs '===' ) is faster?
?: vs && and || operators
|| vs &&
~~ vs ||
if vs && (condition) (false version)
Comments
Confirm delete:
Do you really want to delete benchmark?