Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.includes vs if && vs ternary if &&
(version: 0)
Comparing performance of:
if && vs Array.includes vs Ternary if
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
if &&
const foo = 'foo'; if (foo !== 'bar' && foo !== 'baz') { return 0; } return 1;
Array.includes
const foo = 'foo'; return ['bar', 'baz'].includes(foo) ? 1 : 0;
Ternary if
const foo = 'foo'; return foo !== 'bar' && foo !== 'baz'? 0 : 1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
if &&
Array.includes
Ternary if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
if &&
15636021.0 Ops/sec
Array.includes
9240383.0 Ops/sec
Ternary if
17091358.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, along with the pros and cons of different approaches. **Benchmark Definition** The test case is designed to compare three different ways of checking if a string `foo` is not equal to 'bar' or 'baz'. The options being compared are: 1. **if &&**: Using an if statement with two conditions joined by the logical AND operator (`&&`). 2. **Array.includes**: Using the `includes()` method on an array containing the string. 3. **Ternary if &&**: A ternary operator followed by another if statement with the same conditions as the first option. **Pros and Cons** 1. **if &&**: * Pros: Simple, easy to read, and widely supported. * Cons: May have performance overhead due to the repeated checks. 2. **Array.includes**: * Pros: Efficient, as it only iterates over the array once. * Cons: Requires an additional array to be created, which may not be desirable in all cases. 3. **Ternary if &&**: * Pros: Combines two operations into one, potentially reducing overhead. * Cons: May be less readable than a traditional if statement, and its performance is similar to the other options. **Library Usage** None of the provided benchmark definitions use any external libraries or dependencies. **Special JavaScript Features** None of the test cases use special JavaScript features like async/await, generators, or ES modules. **Benchmark Results** The latest benchmark results show that: * **Ternary if**: Has the highest execution rate (17091358.0 executions per second). * **if &&**: Has a lower but still significant execution rate (15636021.0 executions per second). * **Array.includes**: Has the lowest execution rate (9240383.0 executions per second). The results suggest that the **Ternary if** approach is slightly faster than the other options, likely due to its ability to combine two operations into one. **Alternative Approaches** Some alternative approaches to check if a string is not equal to 'bar' or 'baz' could include: * Using `!=` operator directly on the string. * Using a regular expression with the `test()` method. * Utilizing a custom implementation using a switch statement or a lookup table. However, these alternatives are not being tested in this specific benchmark.
Related benchmarks:
equality vs includes
chain of or equals vs includes but smaller
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?