Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith sz
(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) === 0; 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:
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 benchmark and analyze what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark measures the performance of three string manipulation functions: `Regex`, `.indexOf`, and `.startsWith`. The test creates an array of random strings, each with a length between 1 and 20 characters, and then uses these strings to iterate over the data. Each iteration performs one of the three tests: * `Regex`: Tests if the entire string matches a regular expression. * `.indexOf`: Tests if a specific substring (`match`) is found at the beginning of the string. * `.startsWith`: Tests if a specific prefix (`match`) is present at the start of the string. **Comparison Options** The benchmark compares the performance of these three functions across different browsers and devices: 1. `Regex` 2. `.indexOf` (string indexing) 3. `.startsWith` (string starting with) **Pros and Cons** * **Regex**: Pros: + Can match complex patterns. + Allows for regular expression groups, conditions, and quantifiers. + Can be used for more than just string matching. Cons: + Can be computationally expensive due to the complexity of regex patterns. + May have performance issues with very long strings or large datasets. * `.indexOf`: + Fast and efficient for simple substring searches. + Only needs to scan a portion of the string (from the start). Pros: + Generally faster than `Regex`. Cons: + Requires exact matching, so it may not work well with complex patterns. + May have issues if the target substring is not found in the data. * `.startsWith`: + Similar to `.indexOf`, but starts from the beginning of the string instead of searching for a specific position. Pros: + Fast and efficient when the target prefix is present at the start. Cons: + May be slower than `.indexOf` if the target prefix is not found or appears later in the data. **Other Considerations** * **Performance Impact**: The performance difference between these functions will depend on factors like string length, complexity, and dataset size. In general, `Regex` may perform worse for very long strings or complex patterns. * **Browser Variability**: Results may vary across different browsers due to differences in engine optimization, caching, and other implementation-specific factors. **Library Used** No libraries are explicitly mentioned in the benchmark code. However, it's worth noting that JavaScript engines like V8 (used by Chrome) and SpiderMonkey (used by Firefox) have built-in optimizations for string manipulation operations, which may impact performance. **Special JS Features or Syntax** None of the provided test cases use any special JavaScript features or syntax beyond regular expressions (`Regex`).
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?