Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs startsWith vs indexOf perf on url
(version: 0)
compare url path matching perf
Comparing performance of:
indexOf vs startsWith vs regex
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const urls = ['/pl/sdkjfdhsalkjhas', '/go/lasdkfhaskljdhflaksjdhflaksjhdf', '/FI/FI/OFEJ']; const STARTS_WITH = ['/go/', '/pl']; const EQUALS = ['/FI/FI/OFEJ', '/fi/fi/ofej']; const ex = /^\/(go\/|pl\/|FI\/FI\/OFEJ)\W*/;
Tests:
indexOf
const urls = ['/pl/sdkjfdhsalkjhas', '/go/lasdkfhaskljdhflaksjdhflaksjhdf', '/FI/FI/OFEJ']; const STARTS_WITH = ['/go/', '/pl']; const EQUALS = ['/FI/FI/OFEJ', '/fi/fi/ofej']; urls.map(url => STARTS_WITH.filter((starts) => url.indexOf(starts) !== -1)).length > 0;
startsWith
const urls = ['/pl/sdkjfdhsalkjhas', '/go/lasdkfhaskljdhflaksjdhflaksjhdf', '/FI/FI/OFEJ']; const STARTS_WITH = ['/go/', '/pl']; const EQUALS = ['/FI/FI/OFEJ', '/fi/fi/ofej']; urls.map(url => STARTS_WITH.filter((starts) => url.startsWith(starts) !== -1)).length > 0;
regex
const urls = ['/pl/sdkjfdhsalkjhas', '/go/lasdkfhaskljdhflaksjdhflaksjhdf', '/FI/FI/OFEJ']; const STARTS_WITH = ['/go/', '/pl']; const EQUALS = ['/FI/FI/OFEJ', '/fi/fi/ofej']; const ex = /^\/(go\/|pl\/|FI\/FI\/OFEJ)\W*/; urls.map(url => STARTS_WITH.filter((starts) => url.match(ex) !== -1)).length > 0;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
startsWith
regex
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 JSON and explain what is being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark tests the performance of three different approaches to check if a URL path matches a certain pattern: 1. `startsWith`: Checks if the URL starts with a specific prefix. 2. `indexOf`: Finds the index of the first occurrence of a substring within the URL. 3. `regex`: Uses a regular expression to match the URL against a pattern. **Comparison** The benchmark compares the performance of these three approaches on a set of URLs, including: * `/pl/sdkjfdhsalkjhas` * `/go/lasdkfhaskljdhflaksjdhflaksjhdf` * `/FI/FI/OFEJ` **Options Compared** 1. **startsWith**: Compares the number of executions per second for `startsWith` with each URL. 2. **indexOf**: Compares the number of executions per second for `indexOf` with each URL. 3. **regex**: Compares the number of executions per second for a custom regular expression (`ex`) with each URL. **Pros and Cons** 1. **startsWith**: * Pros: Simple and efficient, as it only checks if the URL starts with a specific prefix. * Cons: May not match URLs that have multiple prefixes or a non-alphabetic first character. 2. **indexOf**: * Pros: Fast and efficient, as it finds the index of the first occurrence of a substring within the URL. * Cons: May return -1 if the substring is not found, which could lead to false positives in some cases. 3. **regex**: * Pros: Highly flexible and powerful, allowing for complex patterns and matching. * Cons: Can be slower than `startsWith` or `indexOf`, especially for simple patterns. **Library Used** None, as this benchmark uses built-in JavaScript functions (`startsWith`, `indexOf`) and a custom regular expression. **Special JS Feature/Syntax** No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** Other approaches to check URL path matching could include: 1. **Substrings**: Similar to `startsWith` but allows for multiple prefixes. 2. **Regex alternatives**: Other regex engines like PCRE (Perl-Compatible Regular Expressions) might be faster or more efficient than the custom regex used here. 3. **URL parsing libraries**: Libraries like URL-Parser or url-parse can parse URLs and extract path information, potentially reducing the complexity of the benchmark. Keep in mind that this is a simplified explanation, and there are many nuances to consider when optimizing JavaScript performance.
Related benchmarks:
split vs regex onurl
URL Origin: startsWith vs Regex various
Regex vs startsWith
RegEx.test vs. String.includes vs. String.match vs. String.indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?