Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf & .lastIndexOf vs .startsWith & .endsWith
(version: 1)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .startsWith
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^test.+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 && str.lastIndexOf(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) && str.endsWith(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 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
779.0 Ops/sec
.indexOf
622.8 Ops/sec
.startsWith
1153.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided compares the performance of three different string searching methods in JavaScript: **Regular Expressions**, **.indexOf() with .lastIndexOf()**, and **.startsWith() with .endsWith()**. Here’s a breakdown of what each method does, their pros and cons, and considerations to keep in mind when choosing one over the others. ### 1. Regular Expressions (`regex.test(str)`) **Description**: This test employs a regular expression to search through randomly generated strings to find a match for the pattern `/^test.+test$/`. - **Pros**: - Very powerful and flexible for complex pattern matching. - Can handle intricate string matching conditions beyond simple substring checks. - **Cons**: - Generally slower compared to simpler string methods due to the overhead of parsing and executing the regex. - Can be less readable for those unfamiliar with regex syntax. ### 2. `.indexOf()` and `.lastIndexOf()` **Description**: This approach uses the `.indexOf()` method to determine if a specific substring (in this case, "test") exists in the string, while `.lastIndexOf()` is included but not required for just checking existence. - **Pros**: - Simple and easy to understand for basic substring searches. - Performs well with large strings when only checking for presence. - **Cons**: - Returns the position of the first or last occurrence, which can be less efficient if only existence matters. - Does not accommodate more complex search patterns that regular expressions handle. ### 3. `.startsWith()` and `.endsWith()` **Description**: This test checks if the string starts or ends with the substring "test". - **Pros**: - Very clear and intuitive to read, especially for checks against the beginning or the end of strings. - Typically faster than the other approaches as it directly checks the positions without searching throughout the entire string. - **Cons**: - Limited to checking only the start or end of strings, so it’s not suitable for general substring searches. - Cannot be used for more complex matching scenarios. ### Benchmark Results From the benchmark results, we see: - `.startsWith()` had the highest performance (approximately 1489.68 executions per second). - Regular Expressions followed with 867.46 executions per second. - `.indexOf()` was the slowest at 560.19 executions per second. ### Other Considerations - The choice of method will depend on the specific use case: - For simple existence checks, `.indexOf()` or `.startsWith()`/`.endsWith()` are recommended due to clarity and speed. - For more complex string matching requirements, regular expressions provide great flexibility. - **Alternatives**: - Other JavaScript methods, such as `String.prototype.includes()`, could also be considered as a modern alternative to `.indexOf()`, which is often more readable and is better suited for general existence checks without requiring the position of the substring. Choosing the right string method is essential for balancing performance, readability, and functionality based on the requirements of the application.
Related benchmarks:
Regex vs .indexOf vs .startsWith vs .substrasdasdasd
Regex vs .indexOf vs .startsWith vs .substr x
Regex vs .indexOf vs .startsWith vs .substr (full)
Regex vs .indexOf vs .startsWith vs .substr (Such)
Regex vs .indexOf vs .startsWith vs .substr
Regex vs endsWith vs indexOf vs lastIndexOf
Regex vs .indexOf vs .startsWith vs .substr vs includes
Regex vs .indexOf vs .startsWith vs .indexof
Regex vs .startsWith vs === vs .indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?