Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf with side effects
(version: 0)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
startsWith vs indexOf start
Created:
4 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:
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]; if (!str.startsWith(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]; if (str.indexOf(match) !== 0) x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
indexOf start
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):
**What is being tested?** MeasureThat.net is testing the performance of three different methods to check if a string matches a certain pattern: 1. `startsWith()`: Checks if the string starts with a specified prefix. 2. `indexOf()` (with a non-zero index): Checks if the string contains a specified substring, starting from a specific position. These tests are designed to evaluate which method is faster for large datasets, while also considering potential side effects or optimizations that might impact performance. **Options compared** The tests compare the performance of the following options: 1. `startsWith()` 2. `indexOf()` (with a non-zero index) vs. `indexOf()` with index 0 3. Using regular expressions (`^` and `$` anchors) **Pros and Cons:** 1. **`startsWith()`**: * Pros: + Simple and efficient implementation. + Well-suited for large datasets, as it only needs to check if the string starts with the prefix. * Cons: + Might not be suitable for exact matching (e.g., checking if a string contains a specific substring). 2. `indexOf()` (with a non-zero index) vs. `indexOf()` with index 0: * Pros: + Can be faster than `startsWith()` for large datasets, as it only needs to check if the substring is present at any position in the string. * Cons: + May introduce additional overhead due to the index calculation and comparison. 3. **Regular Expressions (`^` and `$` anchors)**: * Pros: + Provide exact matching capabilities. * Cons: + Typically slower than other methods, especially for large datasets. **Library and its purpose** In this benchmark, `makeRandomString()` is a custom function used to generate random strings. The library being tested does not rely on any specific third-party libraries beyond JavaScript's built-in functionality. **Special JS feature or syntax** None of the test cases explicitly use special JavaScript features or syntax that would require additional explanation. **Other alternatives** If you're interested in exploring alternative methods for string matching, consider: 1. **Regular Expressions (with `RegExp` object)**: This approach provides more flexibility and exact matching capabilities. 2. **Booleans and bitwise operators**: Some developers might use boolean expressions with bitwise operators (`&&`, `||`, `&`, etc.) to optimize string matching. Keep in mind that these alternatives are not necessarily faster or recommended for this specific benchmark. MeasureThat.net's tests aim to isolate the performance impact of different methods on large datasets, rather than exploring alternative approaches altogether.
Related benchmarks:
Some vs. Filter vs. indexOf vs findIndex
Some vs. Filter vs. indexOf (somefixes)
Some vs. Filter vs. indexOf vs. includes (string version)
easd Some vs. Filter vs. indexOf vs. includes
DTMF: array includes vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?