Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf fork
(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
Created:
5 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; 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) === 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.indexOf(match) === 0; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
regex start
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):
Let's break down the provided benchmark and explain what is being tested, compared options, pros/cons, and other considerations. **Benchmark Purpose:** MeasureThat.net's JavaScript microbenchmark aims to compare the performance of three different string manipulation methods: 1. Regular Expressions (Regex) with `test` method 2. String `startsWith` method 3. String `indexOf` method The benchmark creates a pool of random strings and iterates over them, applying each testing method to determine if a given substring matches a predefined pattern. **Options Compared:** * **Regular Expressions (Regex) with `test` method**: Compares the performance of using a regular expression with the `test` method to check if a string contains a specific substring. * **String `startsWith` method**: Compares the performance of using the `startsWith` method to check if a string starts with a given substring. * **String `indexOf` method**: Compares the performance of using the `indexOf` method to find the first occurrence of a substring in a string. **Pros and Cons:** * **Regex (test) method**: + Pros: High accuracy, can match complex patterns, and is widely used for text processing. + Cons: Can be slow due to the complexity of the pattern, may require more memory, and can lead to performance issues with very large inputs. * **String `startsWith` method**: Pros: Fast, efficient, and easy to use. Cons: May not match complex patterns or substrings, and can return incorrect results for empty strings or strings that don't start with the specified substring. * **String `indexOf` method**: + Pros: Fast, efficient, and widely supported by browsers. Cons: Can be slow if the input string is very large, may not match complex patterns or substrings, and can lead to performance issues. **Library Used:** None explicitly mentioned in the provided code. However, it's likely that these libraries are included in the browser or runtime environment being tested (e.g., Chrome). **Special JS Features/Syntax:** None explicitly mentioned in the provided code. The benchmark uses standard JavaScript syntax and does not include any experimental features. **Other Considerations:** * **Warm-up and Initial Execution**: Some browsers may perform a warm-up phase before executing the benchmark, which can affect results. * **Browser Variability**: Different browsers may have different performance characteristics, which can lead to variations in benchmark results. * **System Resource Usage**: The benchmark's resource usage (e.g., CPU, memory) can impact performance and accuracy. **Alternatives:** Other string manipulation methods that could be tested include: * String `endsWith` method * String `contains` method * Custom implementation using bitwise operations or other advanced techniques Keep in mind that these alternatives may not be as widely supported by browsers or may have different performance characteristics compared to the original methods being benchmarked.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith
JS startsWith vs split upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?