Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String compare v2
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
Array.includes vs Multiple if Conditions
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.includes
const testStrFirst = 'foo'; ['foo', 'bar', 'yop', 'par'].includes(testStrFirst); const testStrSecond = 'bar'; ['foo', 'bar', 'yop', 'par'].includes(testStrSecond); const testStrNotMatch = 'baz'; ['foo', 'bar', 'yop', 'par'].includes(testStrNotMatch); const testStrLast = 'par'; ['foo', 'bar', 'yop', 'par'].includes(testStrLast);
Multiple if Conditions
const testStrFirst = 'foo'; if (testStrFirst == 'foo' || testStrFirst == 'bar' || testStrFirst == 'yop' || testStrFirst == 'par') {} const testStrSecond = 'bar'; if (testStrSecond == 'foo' || testStrSecond == 'bar' || testStrSecond == 'yop' || testStrSecond == 'par') {} const testStrNotMatch = 'baz'; if (testStrNotMatch == 'foo' || testStrNotMatch == 'bar' || testStrNotMatch == 'yop' || testStrNotMatch == 'par') {} const testStrLast = 'par'; if (testStrLast == 'foo' || testStrLast == 'bar' || testStrLast == 'yop' || testStrLast == 'par') {}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes
Multiple if Conditions
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/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
20893386.0 Ops/sec
Multiple if Conditions
198554288.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided benchmark measures the performance difference between two approaches: 1. `Array.includes` method, which checks if an element exists in an array using the `includes()` method. 2. A multiple `if` condition approach, where a string is compared against each possible value in the array. **Options comparison** The main options being compared are: * `Array.includes()`: A built-in JavaScript method for checking if an element exists in an array. * Multiple `if` conditions: An alternative approach using conditional statements to check if the string matches any of the values in the array. **Pros and Cons of each approach** 1. **Array.includes()**: * Pros: + Efficient, as it uses a binary search algorithm under the hood. + Easy to implement and understand for developers familiar with JavaScript. * Cons: + May not be suitable for very large arrays due to potential performance issues. 2. **Multiple `if` conditions**: * Pros: + Can be useful for specific use cases where the array is small or predictable. + May provide better readability for developers unfamiliar with `Array.includes()`. * Cons: + Inefficient, as it involves multiple iterations and comparisons, leading to slower performance. + More prone to errors due to the complexity of conditional statements. **Other considerations** * Both approaches assume that the array is already populated with values. If the array is empty or null, using `Array.includes()` would throw an error, while the multiple `if` condition approach might not behave as expected. * The benchmark results are influenced by factors like JavaScript engine optimizations, array size, and browser/OS version. **Library usage** There is no explicit library mentioned in the provided benchmark definition. However, if a third-party library or framework was used to create the test cases or generate the arrays, it might have affected the performance results. **Special JS feature or syntax** The `includes()` method uses a feature called "iteration" which allows JavaScript engines to optimize its performance by utilizing specialized instructions like SIMD (Single Instruction, Multiple Data) operations. However, this optimization may not be available in older browsers or with specific array configurations. In summary, the benchmark tests the performance difference between using the built-in `Array.includes()` method and a multiple `if` condition approach for matching a string against an array of values. The results provide insight into which approach is more efficient under different conditions.
Related benchmarks:
equality vs includes
=== vs includes
String equals vs String.includes
equals vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?