Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .startsWith
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) !== -1; 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); 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:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
577.3 Ops/sec
.indexOf
17496.7 Ops/sec
.startsWith
1299.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test is designed to compare the performance of three string matching methods: regular expressions (.regex), the .indexOf() method, and the .startsWith() method. **Regular Expressions (.regex)** In this approach, the `^` character in the regex pattern is used as a prefix match, ensuring that only strings starting with "test" are matched. The test case generates 100,000 random strings of varying lengths, each containing at least one instance of the test string. **.indexOf() method** This approach uses the .indexOf() method to search for the test string within each generated string. It does not perform a prefix match and returns the first occurrence of the test string as an index (or -1 if not found). **.startsWith() method** Similar to .indexOf(), this approach uses the .startsWith() method to check if the generated strings start with the test string. Now, let's discuss the pros and cons of each approach: * **Regular Expressions (.regex)**: * Pros: Efficient for prefix matches, can handle complex patterns. * Cons: Can be slower due to pattern compilation, may not perform well on large datasets. * **.indexOf() method**: * Pros: Fast and efficient for simple substring searches. * Cons: May return the wrong index if the test string is part of a larger string (e.g., "hello world" contains "world"), and it can be slow for very large datasets. * **.startsWith() method**: * Pros: Similar to .indexOf(), fast and efficient, but with an additional check for prefix matches. * Cons: May still return incorrect results if the test string is not a prefix of another string (e.g., "hello world"). The performance differences among these approaches are likely due to factors such as: * Pattern compilation time (for regular expressions) * String search algorithm efficiency * Browser or runtime engine optimizations **Special considerations** In this benchmark, no special JavaScript features or syntax are used. However, it's worth noting that the use of `window` variables for global scope is a common practice in MeasureThat.net tests. If you were to modify this test case to include a specific JavaScript feature or syntax, you might consider using: * ES6 features like arrow functions, template literals, or classes * Modern browser-specific APIs (e.g., WebAssembly) **Alternatives** For similar string matching benchmarks, you can explore other libraries or frameworks that provide optimized implementations of these methods. Some examples include: * Lodash: A popular utility library with a `some()` function for checking if an element is in an array. * underscore.js: Another well-known utility library with various string manipulation functions. When creating your own benchmark, consider factors like: * Dataset size and distribution * Browser or runtime engine version * Pattern complexity (for regular expressions) * Algorithmic efficiency
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Regex vs .indexOf vs .startsWith vs .indexof
Comments
Confirm delete:
Do you really want to delete benchmark?