Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs String.includes
(version: 0)
Matching a string against more than one possibility.
Comparing performance of:
Array.includes vs String.includes vs Multiple if Conditions
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = ['foo', 'bar']; var testString = "foo,bar";
Tests:
Array.includes
const testStrFirst = 'foo'; testArray.includes(testStrFirst); const testStrSecond = 'bar'; testArray.includes(testStrSecond); const testStrNotMatch = 'baz'; testArray.includes(testStrNotMatch);
String.includes
const testStrFirst = 'foo'; testString.includes(testStrFirst); const testStrSecond = 'bar'; testString.includes(testStrSecond); const testStrNotMatch = 'baz'; testString.includes(testStrNotMatch);
Multiple if Conditions
const testStrFirst = 'foo'; if (testStrFirst == 'foo' || testStrFirst == 'bar') {} const testStrSecond = 'bar'; if (testStrFirst == 'foo' || testStrFirst == 'bar') {} const testStrNotMatch = 'baz'; if (testStrFirst == 'foo' || testStrFirst == 'bar') {}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes
String.includes
Multiple if Conditions
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The test cases are comparing the performance of three different approaches to match a string against more than one possibility: 1. `Array.includes` 2. `String.includes` 3. Multiple if conditions using a simple equality check (`testStrFirst == 'foo' || testStrFirst == 'bar'`) **Options compared:** * `Array.includes`: uses the `includes` method on an array to find a specific value * `String.includes`: uses the `includes` method on a string to find a specific substring or value * Multiple if conditions using a simple equality check **Pros and Cons of each approach:** 1. **Array.includes**: * Pros: efficient, optimized by browser engines for array operations * Cons: only works with arrays, can be slower for strings with many matches 2. **String.includes**: * Pros: flexible, works with any string, can be faster than multiple if conditions * Cons: may be slower than `Array.includes` for very large strings or arrays, can be less efficient in certain browsers 3. **Multiple if conditions using simple equality check**: * Pros: easy to understand and implement, can be faster for small strings or arrays with few matches * Cons: can lead to performance issues when dealing with larger inputs, may not be optimized by browser engines **Library/Functionality used in test cases:** None of the test cases use any external libraries. However, `Array.includes` and `String.includes` are built-in methods in JavaScript. **Special JS feature/syntax used:** The only special syntax used is the `||` operator for simple equality checks in the "Multiple if Conditions" benchmark case. **Other considerations:** * The test cases use a relatively small input (two strings, "foo" and "bar"), which may not accurately represent real-world scenarios. * The benchmark does not account for edge cases like null or undefined inputs. * The test results are based on Chrome 100 browser engine, so the performance may vary in other browsers. **Alternatives:** Other alternatives to compare could include: * Using regular expressions (regex) instead of `includes` methods * Using a different data structure, such as a Set or Map, to improve lookup efficiency * Implementing a custom lookup function using assembly language or native code for extreme performance optimization Keep in mind that these alternatives would likely require significant changes to the benchmark and test cases.
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?