Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith 2
(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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
524.4 Ops/sec
.indexOf
15471.4 Ops/sec
.startsWith
1485.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark tests three different string manipulation methods: regular expressions (regex), `indexOf()`, and `startsWith()`. The test creates an array of random strings, iterates over them, and applies each method to check if a specific substring (`"test"`) is present in the string. The goal is to measure the performance difference between these three approaches. **Options Compared** 1. **Regex**: Uses a regular expression object (`window.regex = /test/;`) to match the substring `"test"` against each string. 2. `indexOf()`: Searches for the specified substring using the `indexOf()` method, which returns the index of the first occurrence or -1 if not found. 3. `startsWith()`: Checks if the string starts with the specified substring using the `startsWith()` method. **Pros and Cons of Each Approach** 1. **Regex**: * Pros: Efficient for searching patterns in strings, flexible, and powerful. * Cons: Can be slower due to the overhead of compiling a regex pattern, may not work correctly for certain edge cases, and can lead to performance issues if the pattern is complex. 2. `indexOf()`: * Pros: Fast, simple, and widely supported. * Cons: May not be suitable for searching patterns, as it only checks for exact matches. 3. `startsWith()`: * Pros: Simple, fast, and efficient for checking if a string starts with a specific substring. * Cons: May not work correctly for cases where the start of the string is not the first character (e.g., `startsWith("abc", "a")` returns false). **Library/Functionality Used** None of these methods rely on external libraries. However, `indexOf()` and `startsWith()` are built-in methods in JavaScript. **Special JS Features/Syntax** The benchmark doesn't explicitly use any special JS features or syntax that would affect the performance comparison. The focus is on comparing the inherent efficiency of each method. **Other Alternatives** If you're looking for alternative string manipulation methods, consider: 1. `includes()`: Similar to `indexOf()`, but returns true as soon as the substring is found. 2. `endsWith()`: Checks if a string ends with a specific substring. 3. `replace()`: Replaces occurrences of a pattern in a string. 4. `split()` and `join()`: Splits or joins strings based on patterns. These alternatives might offer better performance for specific use cases, but their choice depends on the specific requirements and constraints of your project.
Related benchmarks:
Regex vs .indexOf vs .startsWith
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?