Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Logical vs Ternary perf (javascript)
(version: 0)
Testing the performance of logical assignment vs ternary assignment
Comparing performance of:
LOGICAL approach vs TERNARY approach
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
LOGICAL approach
var result; for (let i = 0; i < 30000; i++) { const num = Math.floor(Math.random() * 1000); result = (num % 2 && 2) || 1; } return result;
TERNARY approach
var result; for (let i = 0; i < 30000; i++) { const num = Math.floor(Math.random() * 1000); result = num % 2 ? 2 : 1; } return result;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
LOGICAL approach
TERNARY approach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
LOGICAL approach
948.9 Ops/sec
TERNARY approach
915.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, the different approaches compared, their pros and cons, and other considerations. **What is being tested:** The provided benchmark tests the performance of two different approaches to perform an assignment operation: 1. Logical assignment (`LOGICAL approach`): This approach uses a logical AND operator `&&` to assign a value to a variable based on a condition. 2. Ternary assignment (`TERNARY approach`): This approach uses a ternary operator `?` and colon `:` to perform the same assignment operation. **Options compared:** The two approaches are being compared in terms of their performance, which is measured by the number of executions per second. **Pros and Cons of each approach:** * **Logical approach (`LOGICAL approach`):** + Pros: - Can be used for more complex logical expressions. - May be easier to read for some developers. + Cons: - May have a higher performance overhead due to the additional logic required. - Requires parentheses to group conditions, which can make code less readable in some cases. * **Ternary approach (`TERNARY approach`):** + Pros: - More concise and readable syntax compared to logical approach. - Can be faster because it avoids the overhead of an additional logical operation. + Cons: - Limited to simple assignment operations (i.e., no more than one value on each side). - May be less readable for developers unfamiliar with ternary operators. **Library and syntax used:** The provided benchmark code uses the following: * `Math.floor(Math.random() * 1000)` to generate a random number between 0 and 999. * The logical AND operator `&&` and its assignment equivalent `2 && 2`. * The ternary operator `?` and colon `:`. **Other considerations:** * Both approaches use a loop that runs 30,000 times, which can lead to cache thrashing and other performance issues. This might not be the best way to benchmark performance. * The benchmark code does not include any error handling or edge cases, which could affect its accuracy. * The test is only run on a single device (Chrome Mobile iOS 111), which may not represent the full range of user devices. **Alternative approaches:** Other approaches to compare in this benchmark might include: * Using `if` statements instead of logical operators for assignment. * Using `switch` statements instead of ternary operators for simple assignment operations. * Implementing a custom assignment operator using JavaScript's `Function.prototype.call()` method. * Comparing the performance of different optimization techniques, such as loop unrolling or caching. Keep in mind that these alternative approaches would likely require significant changes to the benchmark code and may not be relevant to typical use cases.
Related benchmarks:
Math.max/min vs function ternary vs inline ternary
Math.Max() vs Ternary
Math.max/min vs if vs ternary operatorsd
ParseInt vs conditional ~~
Comments
Confirm delete:
Do you really want to delete benchmark?