Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr
(version: 11)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
9 years ago
by:
Registered User
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; }
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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
1363.6 Ops/sec
.indexOf
12052.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is testing four different string comparison methods: regular expressions (`Regex`), `.indexOf`, `.startsWith`, and `.substr`. The test case uses a custom JavaScript library, `window.regex`, which represents a simple regex pattern. The test data consists of 100,000 randomly generated strings with lengths between 1 and 20 characters. **Test Cases** There are two individual test cases: 1. **Regex**: This test case uses the `window.regex` pattern to match against each string in the test data. The benchmark measures the execution time of this operation. 2. **.indexOf**: This test case uses the `.indexOf()` method to search for a substring (the custom regex pattern) within each string in the test data. The benchmark measures the execution time of this operation. **Library: window.regex** The `window.regex` library is not a standard JavaScript library, but rather a custom implementation of a simple regular expression pattern (`/^test/`). Its purpose is to match against strings that start with the literal character "t". In the context of this benchmark, it's used to create a consistent testing scenario. **Options Being Compared** The two test cases are comparing the performance of different string comparison methods: 1. **Regex**: This method uses a regular expression pattern to match against each string. 2. **.indexOf**: This method uses the `.indexOf()` method to search for a substring within each string. **Pros and Cons of Each Approach** Here's a brief analysis of the pros and cons of each approach: * **Regex**: + Pros: Can be more flexible and powerful than simple substring searches. + Cons: Can be slower due to the overhead of regular expression parsing and matching. * **.indexOf**: + Pros: Typically faster than regex, especially for simple substring searches. + Cons: May not be as flexible or powerful as regex. **Other Considerations** The benchmark also considers other factors that might affect performance: * String length: The test data consists of strings with varying lengths, which can impact performance due to string allocation and copying overhead. * Frequency of matching: The regex pattern is only matched against each string once per iteration, whereas the `.indexOf()` method searches for the substring multiple times per iteration. **Alternatives** If you're looking for alternative benchmarking options or want to explore other string comparison methods, consider: * Using standard JavaScript libraries like `String.prototype.includes()` or `Array.prototype.indexOf()`. * Investigating other string matching algorithms, such as Boyer-Moore or Knuth-Morris-Pratt. * Implementing your own custom string comparison algorithm. Keep in mind that the choice of benchmarking tool and approach depends on your specific use case, performance requirements, and testing goals.
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?