Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs regex multiple cases
(version: 0)
Comparing performance of:
regex vs indexof many vs indexof single
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /PERCENT|VARIANCE|TARGET/; window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
regex
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regex; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
indexof many
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf('TARGET') !== -1 || str.indexOf('VARIANCE') !== -1 || str.indexOf('PERCENT') !== -1 ; x += 1; }
indexof single
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf('TARGET') !== -1 ; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
regex
indexof many
indexof single
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 explaining the JavaScript microbenchmark on MeasureThat.net. **Benchmark Definition** The benchmark tests three different approaches to searching for specific substrings in an array of random strings: 1. **Regex**: Using regular expressions (`regex.test(str)`) to search for occurrences of `TARGET`, `VARIANCE`, or `PERCENT` in each string. 2. **IndexOf many**: Iterating over the array and checking if any of the strings contain all three substrings using multiple `indexOf()` calls: `str.indexOf('TARGET') !== -1 || str.indexOf('VARIANCE') !== -1 || str.indexOf('PERCENT') !== -1`. 3. **IndexOf single**: Similar to the previous approach, but only checks for one substring (`str.indexOf('TARGET') !== -1`). **Options Compared** The benchmark compares three different approaches: * **Regex**: Using regular expressions can be efficient when searching for a small set of patterns in strings. * **IndexOf many**: This approach uses multiple `indexOf()` calls to check if any string contains all three substrings. It may not be as efficient as using regex, especially if the array is large or the substrings are common. * **IndexOf single**: This approach only checks for one substring, which might be faster than checking for multiple substrings. **Pros and Cons** * **Regex**: + Pros: Efficient when searching for a small set of patterns in strings. + Cons: Can be slower for larger arrays or more complex searches. * **IndexOf many**: + Pros: May be faster than regex for large arrays with common substrings. + Cons: Requires multiple `indexOf()` calls, which can be slower than a single regex call. * **IndexOf single**: + Pros: Typically the fastest approach, especially when searching for uncommon substrings. + Cons: May not be as efficient when searching for common substrings. **Library and Syntax** The benchmark uses the following libraries and syntax: * `window.regex = /PERCENT|VARIANCE|TARGET/;`: Creates a regex pattern that matches any of the three substrings (`PERCENT`, `VARIANCE`, or `TARGET`) using the `|` character to separate patterns. * `str.indexOf('TARGET') !== -1 || ...`: Uses multiple `indexOf()` calls to check if a string contains all three substrings. **Special JS Features** The benchmark uses no special JavaScript features, such as async/await, ES6 classes, or Promises. **Alternatives** If you're looking for alternative approaches, you might consider: * Using a dedicated string searching library like `string-search` (which is used in some benchmarks on MeasureThat.net). * Implementing your own custom searching algorithm using techniques like Boyer-Moore or Knuth-Morris-Pratt algorithms. * Using a different language or framework that provides built-in support for efficient string searching. Keep in mind that the best approach will depend on the specific requirements and constraints of your use case.
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Regex /i vs .indexOf with tolowerCase once
Comments
Confirm delete:
Do you really want to delete benchmark?