Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .startsWith vs === vs .indexOf
(version: 1)
Testing some things
Comparing performance of:
Regex vs .startsWith vs === vs .indexOf
Created:
2 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; }
.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) === 0; x += 1; }
===
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 === match; 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 (4)
Previous results
Fork
Test case name
Result
Regex
.startsWith
===
.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
515.8 Ops/sec
.startsWith
891.9 Ops/sec
===
9356.4 Ops/sec
.indexOf
10599.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explore what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of four different string comparison methods: 1. Regular Expressions (Regex) 2. `.startsWith` 3. `===` (strict equality operator) 4. `.indexOf` **Options Compared** * Each test case measures the execution time for a single operation on a large dataset of random strings. * The datasets are generated using a combination of uppercase and lowercase letters, digits, and punctuation marks. **Pros and Cons of Each Approach** 1. **Regex**: Regular expressions offer advanced pattern matching capabilities but can be computationally expensive due to their complexity. They're also more prone to errors if not used correctly. However, they provide a high degree of flexibility and accuracy. 2. **.startsWith**: This method checks if a string starts with a given prefix. It's faster than regular expressions because it only needs to check the first few characters. However, it may not be suitable for all use cases, such as checking for substrings or matching patterns in the middle of a string. 3. **=== (strict equality operator)**: This method checks if two values are identical, including type and value. It's fast but can be sensitive to data types and conversions. 4. **.indexOf**: This method finds the index of the first occurrence of a substring within a string. While faster than regular expressions, it may not be suitable for large datasets or complex searches. **Special JS Features** * `getRandomInt` and `makeRandomString` are custom functions used to generate random strings. * The `TOTAL_STRINGS` constant is set to 100,000, which controls the number of iterations in each test case. **Library Used** None. This benchmark doesn't rely on any external libraries or frameworks. **Other Considerations** The benchmark is designed to provide a general understanding of the performance differences between these four string comparison methods. However, it's essential to consider other factors when choosing a method, such as: * Use cases and requirements * Data characteristics (e.g., size, complexity) * Performance constraints * Accuracy and reliability **Alternatives** If you need to compare string comparisons, you might also want to explore other options like: * `.includes` or `String.prototype.includes()` * `String.prototype.localeCompare()` for locale-sensitive matching * `String.prototype.indexOf()` with a regular expression for more advanced pattern matching
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?