Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Ternary vs conditional OR
(version: 0)
Comparing performance of:
Ternary vs Logical
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
this.condition = !!Math.round(Math.random());
Tests:
Ternary
return this.condition ? this.condition : null;
Logical
return this.condition || null;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Ternary
Logical
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Ternary
47284384.0 Ops/sec
Logical
48644564.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON compares two different approaches to conditional expression evaluation in JavaScript: the ternary operator and the logical OR operator. ### Betweeen Ternary and Logical OR 1. **Ternary Operator (`this.condition ? this.condition : null`)** - **Description**: The ternary operator is a shorthand for an `if-else` statement. In this case, it checks whether `this.condition` is truthy. If it is, it returns `this.condition`; otherwise, it returns `null`. - **Pros**: - Explicitly captures the conditional logic, making it clear that something specific is being returned if the condition is satisfied. - Can handle more complex conditions and multiple branches (i.e., can be nested). - **Cons**: - Slightly less concise for simple operations compared to logical operators. - In terms of performance, it may have minor overhead. 2. **Logical OR Operator (`this.condition || null`)** - **Description**: This expression uses the logical OR operator to evaluate `this.condition`. If `this.condition` is truthy, it is returned; otherwise, `null` is returned. - **Pros**: - Concise and easy to read for simple checks like `this.condition || null`. - The idiomatic use of logical operators in JavaScript often leads to cleaner code. - **Cons**: - It can be less clear when more complex logic is required since it relies on JavaScript's type coercion (truthy or falsy behavior). - Not suitable for cases where you need to specify more than two possible outcomes. ### Results Analysis The benchmark results showcase how each approach performed in terms of execution speed in a specific browser environment (Chrome 129 running on Mac OS X 10.15.7). - **Ternary**: 83,861,760 executions per second - **Logical OR**: 82,234,232 executions per second In this benchmark, the ternary operator performed slightly better than the logical OR. The difference, though, might be negligible in many real-world applications, especially considering that performance can vary based on factors such as the JavaScript engine implementation, browser versions, and system resources. ### Alternatives - **If-Else Statements**: Instead of both constructs, a traditional `if-else` statement can be used. However, this would be less concise and is generally not preferred for simple conditional statements due to verbosity. - **Nullish Coalescing (`??`)**: In ES2020, the nullish coalescing operator can also be useful when checking for `null` or `undefined`, as it only defaults if the left-hand side is nullish (i.e., `null` or `undefined`). The equivalent expression could be `this.condition ?? null`. Example: ```javascript return this.condition ?? null; ``` This operator might offer better behavior in cases where you specifically want to handle `null` and `undefined` distinctly. ### Conclusion In this benchmark, we evaluated two common JavaScript approaches to conditionally returning a value. The ternary operator and logical OR both provide effective methods for achieving similar outcomes, with each having its advantages and use cases based on readability, succinctness, and the specific requirements of the situation. Benchmarks like this can highlight potential performance impacts in tight loops or performance-critical sections of applications, although usually developers will prioritize clarity and maintainability in most codebases.
Related benchmarks:
Math.min vs if/else vs ternary operator
Math.max/min vs if vs ternary operator 232323
Math.max vs ternary
Math.max vs ternary (patched)
Math vs Ternary
Math.min vs if/else vs ternary vs or operator
Math.min vs if/else vs ternary operator vs logical or
Math.min vs if/else vs ternary operator vs assing first
Math.max/min vs if vs ternary operatorsd
Comments
Confirm delete:
Do you really want to delete benchmark?