Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find all substrings and get indices
(version: 1)
Comparing performance of:
.matchAll() RegExp vs .matchAll() regex vs .indexOf() with list
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
.matchAll() RegExp
const testString = "This is OpieOP a Twitch Message Kappa Kappa that contains WeirdChamp some emotes! BibleThump WeirdChamp"; const regExp = new RegExp("\\b(Kappa|OpieOP|BibleThump|Keepo|WeirdChamp)\\b", "g"); testString.matchAll(regExp);
.matchAll() regex
const testString = "This is OpieOP a Twitch Message Kappa Kappa that contains WeirdChamp some emotes! BibleThump WeirdChamp"; const regex = /\\b(Kappa|OpieOP|BibleThump|Keepo|WeirdChamp)\\b/g; testString.matchAll(regex);
.indexOf() with list
const testString = "This is OpieOP a Twitch Message Kappa Kappa that contains WeirdChamp some emotes! BibleThump WeirdChamp"; const emotes = ["Kappa", "OpieOP", "BibleThump", "Keepo", "WeirdChamp"]; const results = {}; for (const emote of emotes) { let index = 0; while (index !== -1 && index !== testString.length) { index = testString.indexOf(emote, index + 1); if (results[emote]) results[emote].push(index); else results[emote] = [index]; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
.matchAll() RegExp
.matchAll() regex
.indexOf() with list
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'll break down the provided benchmark and its test cases to explain what's being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Context** MeasureThat.net is a website where users can create and run JavaScript microbenchmarks. The goal is to compare the performance of different approaches to achieve a specific task. **Test Case 1: .matchAll() RegExp** The first test case measures the performance of using a regular expression with `g` flag (global search) to find all occurrences of a pattern in a string. The regular expression used is: ```javascript const regExp = new RegExp("\\b(Kappa|OpieOP|BibleThump|Keepo|WeirdChamp)\\b", "g"); ``` This regex uses word boundaries (`\\b`) to match whole words only, which helps avoid partial matches. **Pros and Cons:** * Pros: + Easy to read and maintain + Works well for most use cases * Cons: + Can be slower than other approaches due to the overhead of creating a RegExp object + May not perform as well for very long strings or complex patterns **Test Case 2: .matchAll() regex** The second test case measures the performance of using a regex string with `g` flag (global search) in a simpler way, without creating a RegExp object. The regex used is: ```javascript const regex = /\\b(Kappa|OpieOP|BibleThump|Keepo|WeirdChamp)\\b/g; ``` This approach is more concise but may have slightly different performance characteristics compared to the first test case. **Pros and Cons:** * Pros: + Faster creation of regex objects + May perform better for very long strings or complex patterns * Cons: + Less readable and maintainable due to the lack of explicit RegExp object creation **Test Case 3: .indexOf() with list** The third test case measures the performance of using a loop with `indexOf()` method to find all occurrences of a pattern in a string. The code uses an array of patterns (`emotes`) and iterates through it, updating indices found in the string. ```javascript const results = {}; for (const emote of emotes) { let index = 0; while (index !== -1 && index !== testString.length) { index = testString.indexOf(emote, index + 1); if (results[emote]) results[emote].push(index); else results[emote] = [index]; } } ``` **Pros and Cons:** * Pros: + Simple to understand and implement + Works well for smaller strings or simpler patterns * Cons: + May be slower than other approaches due to the overhead of multiple `indexOf()` calls **Library Used:** In all test cases, the `String.prototype.matchAll()` method is used, which is a relatively new feature introduced in ECMAScript 2019. It returns an iterator that yields match objects for each match. Other considerations: * **Special JS features/syntax**: None mentioned. * **Alternative approaches**: Other options might include using `Array.prototype.forEach()` or other iteration methods, but these test cases focus on the specific `.matchAll()` method. * **Other alternatives**: MeasureThat.net also allows users to create benchmarks with different languages, such as Python, C++, or Java. Keep in mind that performance differences between these approaches can be significant, especially for large strings or complex patterns. The test results provide valuable insights into the performance characteristics of each approach, helping developers choose the best solution for their specific use case.
Related benchmarks:
Get first and last char on a string
slice vs substring (with no end index)
find until delimeter: split vs indexOf vs regexp
IndexOf vs Manual Search
IndexOf vs Manual Search test
Comments
Confirm delete:
Do you really want to delete benchmark?