Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ternary vs partial ternary function v2
(version: 1)
Comparing performance of:
ternary vs function
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function partial(condition, trueCase, falseCase) { return condition ? trueCase(condition) : falseCase?.(condition) }
Tests:
ternary
const t1 = Math.random() > 0.5 ? true : false;
function
const t2 = partial(Math.random() > 0.5, () => true)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ternary
function
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ternary
88931104.0 Ops/sec
function
86989640.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "ternary vs partial ternary function v2" compares two approaches to evaluating a conditional expression in JavaScript: the traditional ternary operator and an implementation using a partial function. ### Options Compared 1. **Ternary Operator Approach** - **Test Case**: ```javascript const t1 = Math.random() > 0.5 ? true : false; ``` - **Description**: This is a straightforward use of the ternary operator, which checks if a random number (generated by `Math.random()`) is greater than 0.5. It returns `true` if the condition is met and `false` otherwise. - **Performance**: Generally, ternary operations are fast as they're a built-in part of JavaScript syntax and get optimized well by JavaScript engines. 2. **Partial Function Approach** - **Test Case**: ```javascript const t2 = partial(Math.random() > 0.5, () => true); ``` - **Description**: This uses a custom function called `partial` that accepts a condition and two functions (`trueCase` and `falseCase`). It evaluates the condition and executes either `trueCase` or the optional `falseCase` based on the condition. The `falseCase` is called with the condition only if it was provided. - **Purpose of the Library/Function**: In this case, rather than using a conditional statement directly, it abstracts the logic into a function in order to potentially increase flexibility. The `partial` function allows for more complex cases where the actions may be determined by function callbacks rather than static values. ### Pros and Cons - **Ternary Operator** - **Pros**: - Fast and concise. - Clear and simple syntax for basic conditions. - Directly supported and optimized by JavaScript engines. - **Cons**: - Less flexibility for more complex scenarios where multiple operations need to be performed. - Difficult to read if the logic becomes complex or nested. - **Partial Function** - **Pros**: - Enhanced flexibility, allowing for dynamic behavior. - Cleaner approach for more complex conditional logic where callback functions may differ. - **Cons**: - Overhead of function invocation, introducing slight performance costs. - More verbose than the ternary operator for simple conditions, which could be seen as unnecessary complexity in simple cases. ### Other Considerations Using functions like `partial` may improve code readability and maintainability in larger codebases or more complex logic scenarios. However, for simple conditions, the ternary operator generally remains the best choice due to its performance and simplicity. ### Alternatives - **If-Else Statement**: In cases where more complex conditional evaluations are needed, a traditional `if-else` statement can be a viable alternative. It is more verbose than the ternary operator, but also better suited for situations where you need to execute more than one statement in either case. - **Logical AND/OR Operators**: For simple true/false evaluations, logical operators can also be used as a terse alternative, although readability can suffer. In conclusion, the choice between these approaches should depend on the specific use case, weighing performance, readability, and complexity.
Related benchmarks:
If else ternary versus normal
Math.max/min vs ternary operator (in functions)
Math.max/min vs function ternary vs inline ternary
If/Else vs Ternary
Ternary vs Condition
Ternary vs Condition v2
2-if vs && (condition)
ternary vs partial ternary in function
ternary vs partial ternary function
Comments
Confirm delete:
Do you really want to delete benchmark?