Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf (test 2)
(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:
3 years ago
by:
Guest
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; var result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = result !== !!regex.test(str); x += 1; } console.log(result);
startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = result !== str.startsWith(match); x += 1; } console.log(result);
regex end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexEnd; var result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = result !== !!regex.test(str); x += 1; } console.log(result);
endsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = result !== str.endsWith(match); x += 1; } console.log(result);
indexOf start
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = result !== (str.indexOf(match) === 0); x += 1; } console.log(result);
indexOf end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var length = window.matchLength; var result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = result !== (str.indexOf(match) === str.length - length); x += 1; } console.log(result);
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):
I'll explain the benchmark in detail. **What is being tested?** MeasureThat.net is testing the performance of different string comparison methods in JavaScript: 1. `startsWith`: checks if a string starts with a specified substring 2. `endsWith`: checks if a string ends with a specified substring 3. `indexOf` (with and without additional conditions): searches for a substring within a string, returning its index **Comparison options** The benchmark compares the performance of each method on a large dataset of randomly generated strings. **Pros and Cons:** 1. **Regex (`regexStart` and `regexEnd`)**: uses regular expressions to match the start or end of a string. * Pros: + Simple to implement + Fast for short strings * Cons: + Slow for large strings due to the complexity of regex engines + May be affected by regex flags and options 2. `startsWith`: * Pros: + Fast, as it only needs to check the first characters of the string + Simple implementation * Cons: + May not work correctly for non-ASCII strings or strings with Unicode code points above 256 (due to the limit on the number of bytes in the string literal) 3. `endsWith`: * Pros: + Similar performance characteristics to `startsWith` + Simple implementation * Cons: + May not work correctly for non-ASCII strings or strings with Unicode code points above 256 (same issues as `startsWith`) 4. `indexOf` (with and without additional conditions): * Pros: + Fast, especially when searching from the end of the string (as in `indexOfEnd`) * Cons: + May be slower for short strings due to the overhead of the search algorithm 5. Note that the `ExecutionsPerSecond` metric is likely based on the number of iterations performed by each method within a given time frame, rather than actual execution speed. **Device platform and browser** The benchmark results are for Firefox 111 on Windows desktop devices. The device platform and browser can affect performance due to various factors such as hardware capabilities, operating system optimizations, and JavaScript engine differences. **Interpretation of the results** Looking at the results: * `startsWith` and `endsWith` are both relatively fast (higher `ExecutionsPerSecond` values), but slower than `indexOfEnd`. * The `indexOf` method with additional conditions (`indexOfEnd`) is significantly faster than the other methods. * The regex methods (`regexStart` and `regexEnd`) are slower, as expected due to their complexity and overhead. Keep in mind that this benchmark may not be representative of all JavaScript engines or use cases.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith
JS Regex vs .startsWith vs .indexOf but with lowercase when checking index
JS startsWith vs split upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?