Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ternary vs partial ternary in function
(version: 1)
Comparing performance of:
Ternary vs Partial ternary in function
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const n = Math.random(); function partial(condition, trueCase, falseCase) { return condition ? trueCase(condition) : falseCase?.(condition) }
Tests:
Ternary
const t = n > 0.5 ? true : false;
Partial ternary in function
partial(n > 0.5, () => true)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Ternary
Partial ternary in function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Ternary
169535280.0 Ops/sec
Partial ternary in function
174500144.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares two approaches to handling conditional logic in JavaScript, specifically evaluating the performance of a standard ternary operator versus a custom partial function that mimics some ternary behavior. ### What is tested? 1. **Ternary Operator:** - **Code**: `const t = n > 0.5 ? true : false;` - This standard ternary operation checks if the variable `n` (which is randomly generated and between 0 and 1) is greater than 0.5, returning `true` if so, and `false` otherwise. 2. **Partial Ternary in Function:** - **Code**: `partial(n > 0.5, () => true);` - This uses a custom function `partial` that takes a condition and two cases (in this case, `trueCase` is a function that returns `true`, and `falseCase` is not provided explicitly). If the condition is true, `trueCase` executes; if false, `falseCase` could potentially execute if it were defined. ### Options Compared - **Ternary Operator (Test Name: Ternary)** - **Pros**: - Simplicity and readability: The syntax is straightforward and commonly used. - Generally optimized by JavaScript engines, making it very fast in execution. - **Cons**: - Slightly less flexible, as it does not allow for function parameterization. - **Partial Ternary in Function (Test Name: Partial ternary in function)** - **Pros**: - More flexible as it can accept functions as arguments, allowing conditional logic to be extended more dynamically. - Can handle more complex scenarios with disparate behaviors associated with conditions. - **Cons**: - Could add overhead due to additional function calls and execution context switching. - Slightly less intuitive than the standard use of the ternary operator for those not familiar with functional patterns. ### General Observations From the benchmark results: - The **Partial ternary in function** achieved approximately 174.5 million executions per second. - The **Ternary** operation was slightly slower, at about 169.5 million executions per second. - Both methods show impressive performance, highlighting that even custom functions can be efficiently executed in modern JavaScript engines. ### Other Considerations & Alternatives Other alternatives for conditional checks beyond these tested methods include: 1. **If-Else Statements**: - Unlike the ternary operator, which condenses expressions, if-else statements can make complex logic easier to follow but may be less concise. 2. **Logical Operators**: - Using short-circuit evaluation with logical operators (e.g., `condition && trueCase`) can yield readable one-liners but may not provide the same level of control as explicit conditions. 3. **Switch Statements**: - For multiple conditions, switch statements can be a more organized approach but are not suitable for binary decisions as in this case. 4. **Libraries** (if applicable): - No external libraries are used in this context; however, using libraries can simplify complex logic development while sacrificing some performance depending on the library's internal optimizations. In conclusion, the choice between these approaches hinges on the specific use case, desired readability, and potential execute time performance, all vital considerations for software engineers optimizing their conditional logic.
Related benchmarks:
Math.max/min vs ternary operator (in functions)
Math.max/min vs function ternary vs inline ternary
If/Else vs Ternary
Math.max/min vs if vs ternary operator 232323
Math vs Ternary
Math min simple vs ternary greater than
Ternary vs Condition v2
ternary vs partial ternary function
ternary vs partial ternary function v2
Comments
Confirm delete:
Do you really want to delete benchmark?