Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String in list check
(version: 0)
Comparing performance of:
Array.includes() vs Array.indexOf() vs RegExp.test()
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var findthis = "findthis"; var arrayoptions = ["something", "else", "findthis"]; var regex = /^(something|else|findthis)$/;
Tests:
Array.includes()
arrayoptions.includes(findthis);
Array.indexOf()
arrayoptions.indexOf(findthis) > 0;
RegExp.test()
regex.test(findthis);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.includes()
Array.indexOf()
RegExp.test()
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):
I'd be happy to explain what's being tested in this benchmark and the options compared. **Benchmark Overview** The benchmark measures the performance of three different string comparison methods: 1. `Array.includes()` 2. `Array.indexOf()` 3. `RegExp.test()` These methods are used to check if a specific string (`findthis`) is present in an array or if it matches a certain pattern using regular expressions. **Options Compared** The benchmark compares the performance of these three methods on different browsers and devices, specifically: * Chrome 109 * Desktop (Windows) The options compared are: 1. `Array.includes()`: checks if an element with the value `findthis` is present in the array. 2. `Array.indexOf()`: returns the index of the first occurrence of `findthis` in the array. If not found, it returns -1. 3. `RegExp.test()`: tests if the string `findthis` matches the regular expression pattern defined in the script. **Pros and Cons** Here are some pros and cons for each option: * **Array.includes()**: + Pros: simple and efficient, works well with large arrays. + Cons: may be slower than other methods for small arrays due to its linear search approach. * **Array.indexOf()**: + Pros: can return the index of the element if found, which might be useful in some cases. + Cons: has a higher overhead than `includes()` and may not work well with large arrays or arrays that contain many duplicate values. * **RegExp.test()**: + Pros: allows for more complex pattern matching, including negative lookaheads and other features. + Cons: can be slower than the other two methods due to the overhead of compiling and executing regular expressions. **Library Used** The benchmark uses the built-in `Array` object in JavaScript, which is a native array implementation. No external libraries are required for this benchmark. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. The code only uses standard JavaScript syntax and features, such as arrays, regular expressions, and string concatenation. **Alternatives** If you're looking for alternatives to these methods, here are a few options: * For checking if an element is present in an array: + Use `Set` data structure: create a set from the array elements and check if the target value is in the set. + Use `FindIndex()` method: similar to `indexOf()`, but returns -1 instead of index if not found. * For pattern matching using regular expressions: + Use a dedicated regex library like Regexp.js or regex-string. + Use a alternative approach, such as using a trie data structure to store the array values. Keep in mind that these alternatives may have different performance characteristics and use cases, so it's essential to consider your specific requirements before choosing an alternative.
Related benchmarks:
Array.prototype.find vs _.find vs regex
RegEx vs Array.includes
RegEx vs Array.includes v2
RegEx.test vs Array.includes — fork 1
Comments
Confirm delete:
Do you really want to delete benchmark?