Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs substring compare
(version: 0)
Comparing performance of:
regex vs substring
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const link = "https://stats.nba.com"; const linkRegex = new RegExp(/^(http|www)/i);
Tests:
regex
const link = "https://stats.nba.com"; const linkRegex = new RegExp(/^(http|www)/i); if(link.match(linkRegex) !== null){}
substring
const link = "https://stats.nba.com"; const start = link.substring(0,3); if(start === 'htt' || start === 'www'){}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
1931999.9 Ops/sec
substring
39174312.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **What is being tested?** The provided JSON represents two test cases: `regex` and `substring`. Both tests aim to measure the performance difference between using regular expressions (regex) and substring methods for string comparison. The strings being compared are links that start with either "http" or "www". **Options compared:** Two approaches are being compared: 1. **Regex**: Using a regular expression to match the link against a specific pattern. 2. **Substring**: Extracting a subset of characters from the link using the `substring` method. **Pros and Cons:** * **Regex**: + Pros: Regex provides a flexible way to match complex patterns, including non-ASCII characters and varying lengths. It's also more readable when dealing with specific pattern requirements. + Cons: Regex can be slower than substring due to its complexity and overhead of parsing the regular expression engine. * **Substring**: + Pros: Substring is generally faster and more straightforward for simple string comparisons, making it a good choice for basic matching tasks. + Cons: Substring requires manual specification of the start and end indices, which can lead to errors or inconsistencies. **Library usage:** Neither test case explicitly uses a library. However, the `RegExp` class is used in the regex test case, which is a built-in JavaScript API for working with regular expressions. **Special JS features:** No special JavaScript features or syntax are mentioned in this benchmark. The tests focus on basic string comparison and performance measurement. **Benchmark preparation code:** The script preparation code provides a variable `link` with a sample URL and creates a regex object `linkRegex` with the pattern `/^(http|www)/i`. This setup is then reused for both test cases. **Individual test cases:** Each test case measures the execution time of its respective method: 1. **Regex**: The regex test case uses the `match` method to find a match in the link against the `linkRegex` pattern. If a match is found, it indicates that the link starts with either "http" or "www". 2. **Substring**: The substring test case extracts the first three characters from the `link` variable using the `substring(0,3)` method and compares them to specific values ("htt" and "www"). **Other alternatives:** If substring is too simple for your use case, you might consider alternative string comparison methods: * **String.prototype.includes()**: This method provides a more readable way to check if a string includes a certain pattern. * **String.prototype.startsWith()**: Similar to `includes()`, but only checks if the string starts with a specific pattern. However, these alternatives may not provide significant performance gains over substring in simple cases like this benchmark.
Related benchmarks:
substring vs regex
URL Origin: startsWith vs Regex various
Check URL protocol and domain new URL, includes, endWith vs Regex
Regex vs URL
Comments
Confirm delete:
Do you really want to delete benchmark?