Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf Jan
(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 indexOf start vs toLowerCase().startsWith
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexpStart = new RegExp('^p.*', 'i'); 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.regexpStart; 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; }
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.toLowerCase().indexOf(match) === 0; x += 1; }
toLowerCase().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.toLowerCase().startsWith(match); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex start
startsWith
indexOf start
toLowerCase().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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Overview** The provided benchmark measures the performance of three different methods to check if a string starts with a given pattern: `regex.start`, `startsWith`, and `toLowerCase().startsWith`. The benchmark generates 100,000 random strings and checks each one against the patterns using a while loop. The results are compared across different browsers and devices. **Options being compared** 1. **Regex (`regex.start`)**: This method uses a regular expression to check if the string starts with the given pattern. 2. **`startsWith`**: This method directly checks if the string starts with the given substring using the `startsWith` method of JavaScript strings. 3. **`toLowerCase().startsWith`**: This method first converts the string to lowercase and then checks if it starts with the given substring using `startsWith`. **Pros and Cons** 1. **Regex (`regex.start`)**: * Pros: Regular expressions are powerful pattern-matching tools that can handle complex patterns, including negations, repetitions, and character classes. * Cons: Regular expressions can be slow due to their complexity and the need to compile them before use. They also have a steeper learning curve than other methods. 2. **`startsWith`**: * Pros: This method is simple and efficient, as it only requires a single operation to compare the string against the substring. * Cons: This method may not be suitable for complex pattern matching or when working with non-ASCII characters, where the `startsWith` method may return incorrect results due to Unicode normalization issues. 3. **`toLowerCase().startsWith`**: * Pros: This method is efficient and can handle both ASCII and non-ASCII strings, as it converts the string to lowercase before comparison. * Cons: This method requires an additional operation (converting to lowercase) and may not be suitable for complex pattern matching. **Library usage** There are no explicit libraries used in this benchmark. However, the `RegExp` constructor is used to create a regular expression object, which is then used to check if the string starts with the given pattern. **Special JavaScript features or syntax** None of the methods use any special JavaScript features or syntax beyond what's standard for JavaScript strings and regular expressions. **Alternative approaches** If you're interested in exploring alternative approaches, here are some other options: 1. **Use a dedicated string matching library**: Libraries like `string-matching` (Node.js) or `substr` (Browser-based) provide optimized string matching algorithms that might outperform the built-in methods used in this benchmark. 2. **Implement a custom string matching algorithm**: You could implement your own string matching algorithm using techniques like Boyer-Moore or Knuth-Morris-Pratt, which can be more efficient than the built-in methods for specific use cases. In summary, the choice of method depends on your specific requirements and constraints. If you need to match complex patterns or work with non-ASCII strings, regular expressions might be a good choice. For simple string matching, `startsWith` is likely to be sufficient. Converting to lowercase before comparison can provide a good balance between simplicity and efficiency.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf by Jarvis
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith
JS Regex vs .startsWith vs .indexOf but with lowercase when checking index
Comments
Confirm delete:
Do you really want to delete benchmark?