Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf with attribute
(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:
4 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({path: 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.path); 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.path.startsWith(match) === 0; 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.path); 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.path.startsWith(match) === 0; 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.path.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.path.indexOf(match) === str.path.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):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is comparing four different string matching methods in JavaScript: 1. `Regex` (regex start) 2. `startsWith` 3. `Regex` (regex end) 4. `indexOf` with two variations: * `indexOf start`: starts the search from the beginning of the string * `indexOf end`: starts the search from the end of the string **Library and Purpose** There is no external library being used in this benchmark. **String Matching Methods Compared** Here's a brief explanation of each method: 1. **Regex (regex start)**: Uses a regular expression to match the entire string. The `^` symbol indicates the start of the string, and the `/test/` pattern matches any character (`.`) followed by "test". 2. **startsWith**: Checks if the string starts with a given prefix. In this case, the prefix is set to `"test"`. 3. **Regex (regex end)**: Similar to `regex start`, but uses the `$` symbol to indicate the end of the string. 4. **IndexOf**: * `indexOf start`: Starts searching for the pattern at the beginning of the string. * `indexOf end`: Starts searching for the pattern at the end of the string. **Pros and Cons** Here's a brief summary of each method: 1. **Regex (regex start)**: * Pros: Can match any character sequence, flexible, and powerful. * Cons: Can be slow due to the overhead of regular expression parsing and execution. 2. **startsWith**: * Pros: Simple and fast, as it only needs to check if the string starts with a fixed prefix. * Cons: Limited to checking for a specific prefix, can't match arbitrary patterns. 3. **Regex (regex end)**: * Similar pros and cons to `regex start`. 4. **IndexOf**: * Pros: Fast and simple, as it only needs to check if the string contains a fixed pattern at a specific position. * Cons: Can be slow if the search space is large, and can lead to incorrect results if the position is not exact. **Benchmark Results** The benchmark results show the execution rate of each method on Firefox 94 on Windows Desktop. The `ExecutionsPerSecond` value represents the number of iterations per second for each test. The results suggest that: * `startsWith` is the fastest method, as expected. * `Regex (regex start)` and `Regex (regex end)` are relatively slow due to the overhead of regular expression parsing and execution. * `IndexOf` methods (`indexOf start` and `indexOf end`) are slower than `Regex` methods, likely due to the additional overhead of searching for a specific position. Overall, this benchmark provides insights into the performance characteristics of different string matching methods in JavaScript.
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?