Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .startsWith
Created:
8 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) === 0; x += 1; }
.startsWith
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.startsWith(match) === true; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
.indexOf
.startsWith
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.1:latest
, generated one year ago):
Let's break down what's being tested in this benchmark. **What is being tested?** The benchmark compares the performance of four different methods to find a string within another string: 1. Using a regular expression (`regex.test()`) 2. Using the `indexOf()` method 3. Using the `startsWith()` method 4. (Note: There's also a test for `.substr`, but it's not included in the results, so we'll ignore that one.) **How are these methods implemented?** In each test case, a large array of strings (`window.data`) is created, containing 100,000 random strings. Then, in a loop, each method is used to find the string "test" within each string in the array. Here's a quick summary of how each method works: * `regex.test()`: A regular expression (`/test/`) is created and tested against each string in the array using the `test()` method. * `indexOf()`: The `indexOf()` method is used to find the index of "test" within each string, and if it's 0 (i.e., "test" is at the start of the string), then the test passes. * `startsWith()`: Similar to `indexOf()`, but using the `startsWith()` method instead. **Options compared** The main options being compared are: 1. **Regular expressions (`regex.test()`)**: This method uses a pattern-matching engine to search for the string "test". 2. **String methods (`indexOf()` and `.startsWith()`)**: These methods use simple string comparisons to find the substring. **Pros/Cons of each approach** Here's a brief summary: * **Regular expressions (regex)**: + Pros: Powerful, flexible, and can handle complex patterns. + Cons: Can be slow for simple cases, and may introduce performance overhead due to engine overhead. * **String methods (`indexOf()` and `.startsWith()`)**: + Pros: Simple, fast, and efficient for basic string matching. + Cons: Limited flexibility, and may not work well with complex patterns. **Other considerations** Some additional points to consider: 1. **Browser performance**: The benchmark results show significant performance differences between browsers (e.g., Firefox 60 is significantly faster than Chrome). 2. **Library usage**: In this case, no external libraries are used beyond the built-in browser functions. 3. **Test complexity**: This benchmark tests a relatively simple string matching scenario; more complex scenarios may reveal different performance characteristics. **Other alternatives** If you need to find substrings in strings with high performance requirements, consider using: 1. **String search algorithms**: Implementations like Knuth-Morris-Pratt (KMP) or Rabin-Karp can be faster than built-in string methods. 2. **Substring caching**: Implementing a cache for frequently occurring substring searches can improve performance. Overall, this benchmark provides a useful comparison of simple string matching methods in different browsers.
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?