Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .includes
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .includes vs .startsWith
Created:
3 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; }
.includes
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.includes(match); 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 (4)
Previous results
Fork
Test case name
Result
Regex
.indexOf
.includes
.startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
745.2 Ops/sec
.indexOf
15378.1 Ops/sec
.includes
15413.8 Ops/sec
.startsWith
1464.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Overview** The test is comparing the performance of four string comparison methods: 1. `String.prototype.test()` (or `regex.test()`) 2. `.indexOf()` 3. `.includes()` 4. `.startsWith()` All tests use a similar setup: generating a large dataset of random strings, and then iterating over each string in the dataset to perform one of the above comparisons. **Benchmark Options** Here are the options being compared: * `String.prototype.test()`: Tests if a regular expression matches a given string. * `.indexOf()`: Returns the index of the first occurrence of a specified value within a string. * `.includes()`: Returns `true` if a specified value exists within a string, or `false` otherwise. * `.startsWith()`: Returns `true` if a string starts with a specified value, or `false` otherwise. **Pros and Cons of Each Option** Here's a brief summary of the pros and cons of each option: 1. **`String.prototype.test()`**: Pros: Fast for regular expression matching. Cons: * Can be slower than other methods for simple string comparisons. * May not perform well on large datasets or strings with many special characters. 2. `.indexOf()`: Pros: Fast for finding the first occurrence of a value in a string. Cons: * Returns -1 if the value is not found, which can be slower for some use cases. * Can be slow for very large datasets. 3. `.includes()`: Pros: Simple and efficient for checking if a value exists within a string. Cons: * May return false positives (e.g., "hello".includes("world") returns true) if the value is not unique within the string. 4. `.startsWith()`: Pros: Fast and simple for checking if a string starts with a value. Cons: * Returns false if the string does not start with the specified value, which may be slower than other methods. **Library Usage** There is no explicit library usage in this benchmark, but it's worth noting that `String.prototype.test()` relies on the JavaScript regular expression engine. **Special JS Features** There are no special JS features explicitly mentioned in this benchmark. However, it's worth noting that some of these methods may have different behavior or performance characteristics in certain browsers or environments. **Other Alternatives** For string comparison benchmarks like this one, you might also consider testing other methods, such as: * `.slice()` and `.indexOf()` for finding a specific substring within a string * `String.prototype.localeCompare()` for comparing strings with locale-specific formatting * Using a third-party library or implementation for a specific use case (e.g., fuzzy matching) Keep in mind that the performance characteristics of these alternative methods may vary depending on your specific requirements and constraints.
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?