Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf with more complex expressions
(version: 2)
Find where "via" or "mot" is in a string, and return all words from the start of the string to that point. Otherwise, return the entire string.
Comparing performance of:
Regex vs .indexOf
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
const data = window.data = [ "Jørpeland", "Stavanger via UiS-SUS", "Stavanger", "Randaberg via UiS", "796", "776", "Oslo bussterminal", "Sandnes via SUS-UiS", "Forus via Stavanger", "Åsveien via Sandvika", "Risavika via Stavanger-Varatun", "Stavanger via UiS", "Flyplassen", "Risavika via Stavanger-Smeaheia", "Haugesund bussterminal", "Hjelmeland", "Kristiansand rutebilstasjon", "Dale", "Bogafjell", "Mortavika", "Sundelia via Stavanger", "Sandnes via flyplassen", "Viste Hageby via Stavanger", "B775-BE", "Madlakrossen via SUS-Madlamark", "Sandnes", "Kvernaland - Øksnavadporten", "Bergen", "762", "Bleikemyr via Haugesund sentrum", "Hinna via SUS", "Grinde via Amanda", "Sandnes via Stavanger-Smeaheia", "Stokka", "Etne", "Skaarlia", "Stavanger via SUS", "Amanda via Haugesund sentrum", "3054", "Ålgård - Fiskebekk", "Skudeneshavn via Kopervik og Veavågen", "Vassøy - Kalvøy", "Sandnes via UiS", "Madlakrossen via SUS-Tjensvoll", "Vassøy", "779", "Haugesund via Kopervik", "Hovland - Egersund", "Rosenli via Madlamark-SUS", "3053", "Kleppekrossen - Klepp stasjon", "Stokka via UiS", "797", "778", "Haugesund via Amanda", "Randaberg", "Madlasandnes", "Hundvåg vest/øst", "Vikjå", "Haugesund via Veavågen og Kopervik", "Stavanger via Madlamark-SUS", "Rosenli via Tjensvoll-SUS", "Stavanger Airport via Forum", "Heng", "Ormøy", "Skipavik", "Vormedal via Haugesund sentrum", "Godeset via SUS", "Riskafeltet - Hommersåk", "Sandnes - Hommersåk", "Grødem", "Haugesund", "Gausel stasjon", "Sola via UiS", "Arsvågen", "Austre Åmøy via Bru-Vestre Åmøy kai", "Kristiansand/Stavanger", "Ølen via Vikebygd", "Hundvåg øst/vest", "Åkrehamn via Kopervik", "Sandnes via Stavanger-Varatun", "Nedstrand", "Ropeid - Ølen", "Forus - Ålgård - Fiskebekk", "Ravndal - Oltedal - Gilja", "3052", "Egersund - Hauge i Dalane", "Stavanger sentrum via Forum", "Haugesund via Nes", "Kolnes via Amanda", "3056", "3051", "Hjelmeland via Fister", "3152", "B775-BF", "Rennesøy via Randaberg", "Tananger via flyplassen", "Ropeid - Sauda", "Kristiansand/Oslo", "Vardenes", "Ganddal", "Hundvåg", "Bryne", "Tananger", "Solbakk via Voster", "Fogn", "Jåttåvågen via SUS", "Mekjarvik", "Tau", "Stavanger via Randaberg", "Nesvik - Skipavik", "Jelsa", "Hebnes" ]
Tests:
Regex
const TOTAL_STRINGS = window.TOTAL_STRINGS; const data = window.data; let x = 0; while (x < TOTAL_STRINGS) { const str = data[x++]; const match = str.match(/^(.+?(?:(?=\b(via|mot)\b)|(?!.*\b(via|mot)\b).*$))/); const result = match ? match[0].trim() : str; }
.indexOf
const TOTAL_STRINGS = window.TOTAL_STRINGS; const data = window.data; const match = ['via', 'mot']; let x = 0; while (x < TOTAL_STRINGS) { const str = data[x++].split(' '); let k = 0; let i = -1; while (k < match.length && i === -1) { i = str.indexOf(match[k++]); } const result = (i !== -1 ? str.slice(0, i - 1) : str).join(' '); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.indexOf
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; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
2769995.0 Ops/sec
.indexOf
2771627.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance of two different approaches to find words in a string: using a regular expression (`Regex`) and using the `indexOf` method with multiple search values. **Options Compared** 1. **Regex**: Uses a regular expression to match words starting with "via" or "mot". The regular expression uses the `(.*?)` capture group to match any character (except newline) zero or more times, followed by an optional word boundary (`\b`) and either "via" or "mot". 2. **.indexOf**: Uses the `indexOf` method to search for each of the strings "via" and "mot" in the input string. The `indexOf` method returns the index of the first occurrence of the specified value, or -1 if not found. **Performance Comparison** The benchmark measures the execution time of each approach on a set of test cases. The results show that: * The `Regex` approach is slightly faster than the `.indexOf` approach, with an average execution time of 777440.75 executions per second for Firefox Mobile 103. * The performance difference between the two approaches is relatively small, indicating that both methods are efficient. **Implementation Notes** The benchmark implementation uses a simple text data set stored in the `window.data` variable. The test cases iterate over the data set using a `while` loop, applying each approach to extract words starting with "via" or "mot". In the `.indexOf` approach, the `indexOf` method is called multiple times for each search value, which may lead to slower performance compared to the single regular expression match in the `Regex` approach. **Conclusion** The benchmark highlights the trade-offs between using a regular expression and the `indexOf` method when searching for specific patterns in strings. While both approaches have their strengths and weaknesses, the `Regex` approach is slightly faster in this particular scenario. However, the performance difference may not be significant enough to warrant the use of a regular expression over a simple string search.
Related benchmarks:
slice vs substr vs substring (with end index & large string)
Literal regexp's vs string and array methods
IndexOf vs Includes in string - larger string edition
Javascript: Case insensitive string comparison indexOf vs includes 2
Comments
Confirm delete:
Do you really want to delete benchmark?