Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Null assignment vs Condition Check
(version: 0)
Comparing performance of:
Null assignment vs Conditional Check
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Null assignment
const obj = {}; for (let i = 0; i < 1000000; i++) { obj.array ??= []; }
Conditional Check
const obj = {}; for (let i = 0; i < 1000000; i++) { if (typeof obj.array === 'undefined') { obj.array = []; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Null assignment
Conditional Check
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 benchmark and explain what's being tested. **What is being tested?** The benchmark measures the performance difference between two approaches: null assignment (`obj.array ??= []`) and condition check (`if (typeof obj.array === 'undefined') { ... }`). **Options compared:** * Null assignment using optional chaining (`??=`): This approach uses a single statement to assign a value to `obj.array`, which is only executed if `obj.array` is null or undefined. This allows for more concise code and can be faster than a separate condition check. * Condition check with explicit assignment: This approach checks if `obj.array` is undefined before assigning a value to it, using an `if` statement. **Pros and cons of each approach:** * Null assignment: + Pros: More concise code, potentially faster execution, and less chance of typos or errors. + Cons: May not work as expected in certain edge cases (e.g., if `obj.array` is a string that represents an empty array). * Condition check: + Pros: Works in more edge cases, including when `obj.array` is a string that represents an empty array. However, the code is slightly longer and may be slower due to the additional checks. + Cons: More verbose code, potentially slower execution. **Library usage:** There is no explicit library mentioned in the benchmark definitions or the latest benchmark results. However, both approaches rely on JavaScript's built-in features, such as optional chaining (`??=`) and the `typeof` operator. **Special JS feature/syntax:** The benchmark uses optional chaining (`??=`), which was introduced in ECMAScript 2019 (ES10). This feature allows you to safely access nested properties of an object without worrying about null or undefined values. If your target browser is older than ES10, this approach may not work as expected. **Other alternatives:** If you need to support older browsers that don't have optional chaining or are unable to run the `if (typeof obj.array === 'undefined')` check, alternative approaches might include: * Using a separate variable to store the result of the condition check and assigning it only if the condition is true. * Using a function call instead of an assignment statement, which can help avoid potential issues with null or undefined values. For example: ```javascript const array = obj.array || []; ``` This approach uses the logical OR operator (`||`) to provide a default value for `array` if it's null or undefined.
Related benchmarks:
Which equals operator (== vs ===) is faster? check for null
Testing for false vs undefined vs == null vs hasOwnProperty vs hasOwn for undefined member
Testing for false vs undefined vs == null vs prototype.hasOwnProperty vs hasOwn for undefined member
Boolean constructor vs in Equality check in javascript
nullish assignment vs if 2
Comments
Confirm delete:
Do you really want to delete benchmark?