Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String searching methods
(version: 0)
Filtering large-ish arrays using various string comparison methods.
Comparing performance of:
includes vs indexOf vs regex match vs regex match case-insensitive
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// Build array // Search term const string = 'bingo';
Tests:
includes
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['bingo', 'bingo', 'bingo', 'bingo', 'bingo']); array.filter(i => i.includes('bingo'));
indexOf
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['bingo', 'bingo', 'bingo', 'bingo', 'bingo']); array.filter(i => i.indexOf('bingo') !== -1);
regex match
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['bingo', 'bingo', 'bingo', 'bingo', 'bingo']); array.filter(i => i.match(/bingo/));
regex match case-insensitive
const array = new Array(1000).fill(null).map(() => 'no match found').concat(['bingo', 'bingo', 'bingo', 'bingo', 'bingo']); array.filter(i => i.match(/bingo/i));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes
indexOf
regex match
regex match case-insensitive
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 break down the provided benchmark and its test cases. **Benchmark Overview** The website "MeasureThat.net" provides a platform for creating and running JavaScript microbenchmarks. In this case, we have a benchmark that tests various string comparison methods in JavaScript. The goal is to filter a large array of strings using different approaches. **Script Preparation Code** The script preparation code provided is: ```javascript // Build array // Search term const string = 'bingo'; ``` This code creates an array with 1000 elements, where each element contains the string "no match found", except for the last 5 elements which contain the search term "bingo". The script then uses these arrays to test various string comparison methods. **Test Cases** There are four test cases: 1. **includes**: This test case uses the `includes()` method to filter the array. 2. **indexOf**: This test case uses the `indexOf()` method to filter the array. 3. **regex match**: This test case uses a regular expression to match the search term in the array elements. 4. **regex match case-insensitive**: This test case uses a regular expression with the `/i` flag (case-insensitive mode) to match the search term in the array elements. **Library Usage** There is no explicit library usage in these test cases. However, it's worth noting that the `includes()` and `indexOf()` methods are built-in JavaScript methods, which means they don't rely on any external libraries. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax beyond what's standard in JavaScript (e.g., no ES6+ features like async/await). **Pros and Cons of Different Approaches** Here are some pros and cons of each approach: 1. **includes()**: Pros: * Simple to implement * Fast execution time Cons: * May not be as efficient for large arrays due to the use of `String.prototype.includes()` internally, which has to search through the entire string. 2. **indexOf():** Pros: * Can return an index value, which might be useful in some scenarios Cons: * May have performance issues for very large arrays due to the need to find the first occurrence of the substring. 3. **regex match**: Pros: * Flexible and can match patterns with flags (e.g., `/bingo/i` for case-insensitive matching) Cons: * Can be slower than other methods due to the overhead of compiling regular expressions. 4. **regex match case-insensitive**: Same pros and cons as above, but with the added benefit of ignoring case when matching. **Other Alternatives** Some alternative string comparison methods in JavaScript include: 1. `indexOf()` without the `/i` flag (case-sensitive) 2. `lastIndexOf()` for finding the last occurrence of a substring 3. `split()` and array indexing to extract substrings from an array These alternatives might be worth considering depending on the specific requirements of your use case. I hope this explanation helps!
Related benchmarks:
Large Array Case-Insensitive String Filtering
indexOf vs includes vs some - 20211114
array indexOf vs includes vs some vs binary search on large array
array indexOf vs includes vs some w/ largeish array
Comments
Confirm delete:
Do you really want to delete benchmark?