Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .includes vs .startsWith
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .includes vs .startsWith
Created:
4 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:
14 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
766.8 Ops/sec
.indexOf
19913.2 Ops/sec
.includes
19879.3 Ops/sec
.startsWith
1556.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Overview** The benchmark is comparing the performance of four string manipulation methods: `regex.test()`, `.indexOf()`, `.includes()`, and `.startsWith()` on a large dataset of randomly generated strings. The goal is to determine which method is the fastest. **Test Cases** Each test case consists of a single line of code that performs the specified string manipulation operation on a random string from the dataset. The operations being compared are: 1. `regex.test()`: Tests if a regular expression matches a given string. 2. `.indexOf()`: Returns the index of the first occurrence of a substring within a string. 3. `.includes()`: Returns true if a string contains a specified value, otherwise false. 4. `.startsWith()`: Returns true if a string starts with a specified value. **Pros and Cons** * `regex.test()`: + Pros: Can perform complex pattern matching, flexible, and efficient for most use cases. + Cons: Can be slower than other methods due to the overhead of compiling regular expressions. * `.indexOf()`, `.includes()`, and `.startsWith()`: + Pros: Fast and simple implementations that are suitable for common string comparisons. + Cons: May not handle edge cases well, such as null or undefined inputs. **Library** The `window.regex` variable is set to a regular expression (`/^test/`) which will be used in the first test case. This suggests that the benchmark is testing the performance of regular expressions against other string manipulation methods. **Special JS Features/Syntax** There are no special JavaScript features or syntax being used in this benchmark. The code uses standard JavaScript syntax and does not rely on any modern features like async/await, arrow functions, or destructuring. **Other Alternatives** If `regex.test()` is not suitable for your use case, you can consider using other string manipulation methods such as: * Using a different library or implementation, such as `String.prototype.match()` or `String.prototype.search()`. * Implementing your own regular expression engine. * Using a different programming language that provides similar functionality. In the context of this benchmark, the choice of method depends on the specific use case and performance requirements.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
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?