Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
birirbibir
(version: 0)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
regex start vs startsWith vs regex end vs endsWith vs indexOf start vs indexOf end
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/; window.regexEnd = /test$/; window.match = 'test'; window.matchLength = window.match.length; 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 start
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexStart; 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); x += 1; }
regex end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexEnd; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
endsWith
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.endsWith(match); x += 1; }
indexOf start
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; }
indexOf end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var length = window.matchLength; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match) === str.length - length; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
regex start
startsWith
regex end
endsWith
indexOf start
indexOf end
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question is a fork of an existing benchmark, which compares the performance of different string matching methods: `indexOf` with start and end positions, `startsWith`, `endsWith`, `regexStart`, and `regexEnd`. **Tested Methods** The benchmark tests six different methods: 1. **`indexOf(start)`**: Tests the method `str.indexOf(match) === 0`. This checks if the matched substring is at the beginning of the original string. 2. **`startsWith(match)`**: Tests the method `str.startsWith(match)`. This checks if the original string starts with the matched substring. 3. **`endsWith(match)`**: Tests the method `str.endsWith(match)`. This checks if the original string ends with the matched substring. 4. **`regexStart`**: Tests the method `regex.test(str)`. This uses a regular expression to match the entire string. 5. **`regexEnd`**: Tests the method `regex.test(str)`, but with a different regular expression that only matches at the end of the string. 6. **`indexOf(end)`**: Tests the method `str.indexOf(match) === str.length - length`. This checks if the matched substring is at the end of the original string, given the length of the matched substring. **Comparison and Performance** The benchmark measures the number of executions per second for each method on a dataset of 100,000 random strings. The results are: * `indexOf(end)`: approximately 13144 executions per second * `indexOf(start)`: approximately 13066 executions per second * `startsWith`: approximately 507 executions per second * `endsWith`: approximately 573 executions per second * `regexStart`: approximately 700 executions per second * `regexEnd`: approximately 670 executions per second **Interpretation** The results suggest that: * `indexOf(end)` is the fastest method, followed closely by `indexOf(start)`. * `startsWith` and `endsWith` are significantly slower than the other methods. * The regular expression-based methods (`regexStart` and `regexEnd`) are relatively fast, but not as fast as the `indexOf` methods. **Conclusion** The benchmark highlights the importance of choosing the right string matching method for your specific use case. While `indexOf(end)` is the fastest method in this scenario, other methods may be more suitable depending on your requirements. Regular expression-based methods can offer a good balance between performance and expressiveness, but may require more overhead than simpler methods like `startsWith` or `endsWith`.
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
.substr vs .startwith
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
JavaScript: Regex vs .startsWith vs .indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?