Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
nothing vs optional chaining
(version: 0)
How to better null-check
Comparing performance of:
&& vs optional chaining (?.)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var job = { "summary": { "total_actions": 4, "finished_actions": 4 }, "updated_at": "2020-07-15T10:24:42.364000000Z", "job_id": "4b1733e7-c8e0-4fdd-a7f1-13d3ab725c92", "description": "Job from our export", "created_at": "2020-07-15T10:24:40.975000000Z", "status": "finished", "tags": { "export_id": [ "8848c5d8-fd44-43c0-b7f2-1428d0f4891a" ] } }
Tests:
&&
if (job.tags) { // do nothing }
optional chaining (?.)
if (job?.tags?.export_id) { // do nothing }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
&&
optional chaining (?.)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
&&
1767938048.0 Ops/sec
optional chaining (?.)
677555328.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Definition** The benchmark is comparing two approaches to null-checking in JavaScript: 1. **Traditional null check**: `if (job.tags) { // do nothing }` 2. **Optional chaining**: `if (job?.tags?.export_id) { // do nothing }` **Options being compared** The two options are being compared in terms of their performance. * **Tradsitional null check**: This approach checks if the `job` object has a `tags` property, and then checks if that property is an array. If both conditions are true, it executes the code inside the `if` statement. * **Optional chaining**: This approach uses the optional chaining operator (`?.`) to access nested properties of the `job` object in a safe and efficient way. It returns `undefined` if any of the intermediate steps fail, avoiding unnecessary null checks. **Pros and Cons** **Traditional null check:** Pros: * Simple and easy to understand * Works well for simple cases where only a few properties need to be checked Cons: * Can lead to unnecessary null checks and slower performance when dealing with complex object hierarchies * Can cause errors if the object is not in the expected state (e.g., `job.tags` is `null` or `undefined`) **Optional chaining:** Pros: * Safe and efficient way to access nested properties without fear of null pointer exceptions * Can improve performance by avoiding unnecessary checks Cons: * May require additional imports or configurations if not enabled in older browsers or environments * Can be less intuitive for developers who are not familiar with the optional chaining syntax **Library** In this benchmark, there is no explicit library mentioned. However, it's worth noting that some browsers (e.g., Internet Explorer) have implemented support for optional chaining through the `?.` operator. **Special JS feature or syntax** The benchmark uses the optional chaining operator (`?.`) which was introduced in ECMAScript 2020 as a proposal. It is supported by most modern JavaScript engines, including V8 (used by Google Chrome), SpiderMonkey (used by Firefox), and Node.js. **Other alternatives** If you want to avoid using optional chaining, you can use other approaches such as: * Using the `in` operator to check if a property exists: `if ('tags' in job && Array.isArray(job.tags)) { // do nothing }` * Using the `hasOwnProperty()` method: `if (job.hasOwnProperty('tags') && Array.isArray(job.tags)) { // do nothing }` However, these alternatives can lead to less efficient and more verbose code. Overall, the benchmark is designed to measure the performance difference between traditional null checks and optional chaining in JavaScript.
Related benchmarks:
Map and Filter vs forEach test
&& vs optional chaining
optional chaining vs. non-optional chaining
&& vs optional chaining vs nothing
Comments
Confirm delete:
Do you really want to delete benchmark?