Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Regex vs .startsWith vs .indexOf simpler
(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:
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:
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex start
632.6 Ops/sec
startsWith
1040.5 Ops/sec
indexOf start
3426.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares three string manipulation methods: 1. Regular Expressions (Regex) 2. `startsWith` method 3. `indexOf` method with an offset of 0 (i.e., `str.indexOf(match) === 0`) These methods are used to test whether a given string matches or contains a specific pattern. **Options Compared** The benchmark compares the performance of each method on a dataset of randomly generated strings, which are stored in the `window.data` array. The `window.match` variable represents the target pattern to be matched. Here's a brief description of each option: 1. **Regex**: This method uses a regular expression to match the entire string against the pattern. 2. **`startsWith`**: This method checks if the string starts with the given pattern. 3. **`indexOf` start**: This method checks if the string contains the given pattern at the beginning (offset of 0). **Pros and Cons** Here are some pros and cons for each method: 1. **Regex**: * Pros: flexible, powerful, can match complex patterns. * Cons: slower than `startsWith` and `indexOf`, may have performance implications due to the overhead of compiling regular expressions. 2. **`startsWith`**: * Pros: fast, simple, and efficient for basic string matching. * Cons: may not work correctly if the pattern is not at the beginning of the string (e.g., "test" in "testing"). 3. **`indexOf start`**: * Pros: similar to `startsWith`, but allows for more flexibility (e.g., checking if a substring starts with the given pattern). * Cons: may be slower than `startsWith` due to the overhead of searching for the offset. **Libraries and Special JS Features** None of the methods rely on specific libraries or advanced JavaScript features, apart from regular expressions which are part of the built-in JavaScript API. However, it's worth noting that some browsers may optimize their regular expression engines differently, potentially affecting performance. **Other Considerations** * **Dataset**: The benchmark uses a dataset of randomly generated strings to minimize the impact of any particular string pattern. * **Data size**: The dataset has 100,000 strings, which is a relatively large dataset for a microbenchmark. * **Execution count**: The benchmark measures executions per second (ExecutionsPerSecond) as a performance metric. **Alternatives** Some alternatives to measure JavaScript performance include: 1. **JavaScript performance testing frameworks**: Such as Jest, Mocha, or Cypress, which provide more comprehensive features for measuring and optimizing code performance. 2. **Browser-specific benchmarks**: Like Benchmark.js or js-benchmark, which allow you to create custom benchmarks tailored to specific browser versions or platforms. Keep in mind that the choice of benchmarking tool depends on your specific use case and requirements.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf
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?