Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substrasdasdasd
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^test/; window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 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
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match.toUpperCase()) === 0; x += 1; }
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:
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case, where users can compare the performance of different string search approaches: regular expressions (`regex`), `.indexOf()`, and `.startsWith()`. **Tested Options** The benchmark compares two options for each approach: 1. **Regular Expressions (Regex)**: `window.regex = /^test/;` 2. **.indexOf()**: `str.indexOf(match.toUpperCase()) === 0;` 3. **.startsWith()**: Not explicitly tested, but implied as an alternative to `.indexOf()`. **Approach Comparison** Here's a brief analysis of each approach and their pros and cons: 1. **Regular Expressions (Regex)**: * Pros: Can match complex patterns, flexible, and powerful. * Cons: May be slower due to the overhead of compiling regular expressions, and can be more prone to errors. 2. **.indexOf()**: * Pros: Fast, efficient, and widely supported. * Cons: Only checks for a single character at the beginning of the string (no partial matches), and may not work as expected with non-ASCII characters. The benchmark suggests that `.indexOf()` is faster than regular expressions in this specific test case, which might be due to the simplicity of the pattern (`^test`) or the limited number of strings being tested. **Libraries Used** None are explicitly mentioned in the provided code. However, some external libraries (e.g., jQuery) may be used indirectly through the use of modern JavaScript features and polyfills. **Special JS Features/Syntax** The benchmark uses: 1. **Template literals**: Used for string interpolation (`const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";`). 2. **Arrow functions**: Used for concise function definitions (e.g., `x => x < TOTAL_STRINGS`). These features are widely supported in modern JavaScript environments. **Other Alternatives** If you wanted to compare the performance of other string search approaches, some alternatives could include: 1. **.startsWith()**: As mentioned earlier, not explicitly tested but implied as an alternative to `.indexOf()`. 2. **String.prototype.search()**: A more traditional approach that searches for a regular expression pattern in a string. 3. ** Boyer-Moore Algorithm**: An optimized string searching algorithm that can be used instead of `.indexOf()` or regular expressions. Keep in mind that the best approach will depend on the specific use case and requirements of your project.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?