Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
If vs ?.
(version: 0)
Comparing performance of:
if vs ?.
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var { onClick } = {}
Tests:
if
// 1 if (onClick) { onClick() }
?.
// 2 onClick?.()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
if
?.
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
if
80689000.0 Ops/sec
?.
82192000.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between two approaches: `if` statements and optional chaining (`?.`) for accessing properties or methods. Specifically, it tests the execution time of a single line of code that calls a function named `onClick`. **Script Preparation Code** ```javascript var { onClick } = {}; ``` This script prepares an object literal with a single property called `onClick`. The `{ }` syntax creates an empty object, and `var { onClick } = {};` destructures the object to extract only the `onClick` property. This setup is used to ensure that both test cases have a reference to the `onClick` function. **Html Preparation Code** The `Html Preparation Code` field is empty, which means no HTML is being rendered or executed before running the benchmark. **Test Cases** There are two test cases: 1. **if**: The benchmark definition code for this case is: ```javascript // 1 if (onClick) { onClick(); } ``` This is a traditional `if` statement that checks if the `onClick` property exists and has a truthy value before executing its associated function. 2. **?.**: The benchmark definition code for this case is: ```javascript // 2 onClick?.(); ``` This uses optional chaining (`?.`) to access the `onClick` property and call it only if it exists. If `onClick` is null or undefined, the expression will return `undefined` without executing the function. **Library** There is no explicit library mentioned in the benchmark definition or test cases. However, it's worth noting that optional chaining (`?.`) was introduced in ECMAScript 2020 (ES2020). **Special JavaScript Feature/Syntax** Optional chaining (`?.`) is a relatively recent feature in JavaScript, introduced in ES2020. It allows you to access nested properties or call methods on an object without throwing errors if the property does not exist. Pros of using optional chaining: * Reduces the risk of null pointer exceptions * Simplifies code and improves readability Cons of using optional chaining: * May be less familiar to developers who are not used to it * Can make debugging more challenging due to the absence of error messages when `?.` returns `undefined` **Other Alternatives** If you want to implement a similar approach to optional chaining, but with explicit checks, you can use a conditional statement like this: ```javascript if (onClick) { onClick(); } ``` This approach provides more control over the execution flow but may be less concise and efficient than using `?.`. Another alternative is to use a null-aware operator (`??`), introduced in ES2022, which returns the first operand if it's not null or undefined: ```javascript onClick ?? () => {}; ``` However, this approach is more complex and less straightforward than optional chaining. In summary, the benchmark measures the performance difference between two approaches: traditional `if` statements and optional chaining (`?.`). The choice of approach depends on your personal preference, coding style, and the specific requirements of your project.
Related benchmarks:
Nullish coalescing vs logical OR operators
typeof === function VS function empty
if(!variable) vs if(variable===undefined) performance
if(typeof <var> ===undefined) vs if(<var>)
!! vs default if statement
Comments
Confirm delete:
Do you really want to delete benchmark?