Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Simple Regex 2
(version: 0)
Comparing performance of:
String handling vs Simple Regex vs Anchored Regex
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const urls = [ "/v1/tron/instruments/PVCM/composition", "/v1/tron/instruments/Otro/composition", "/v1/tron/instruments/Serabe/composition", "/v1/tron/instruments/PVCM/compositon", ]; const results = ['PVCM', 'Otro', 'Serabe', false];
Tests:
String handling
const urls = [ "/v1/tron/instruments/PVCM/composition", "/v1/tron/instruments/Otro/composition", "/v1/tron/instruments/Serabe/composition", "/v1/tron/instruments/PVCM/compositon", ]; const results = ['PVCM', 'Otro', 'Serabe', false]; const extractSegment = (url) => { if (!url.startsWith('/v1/tron/instruments/')) { return false; } if (!url.endsWith('/composition')) { return false; } const fragment = url.substring('/v1/tron/instruments/'.length, url.length - '/composition'.length); if (fragment.includes('/')) { return false; } return fragment; } urls.forEach((url, idx) => { const fragment = extractSegment(url); if (fragment !== results[idx]) { throw `Mismatch #{fragment} #{results[idx]}`; } });
Simple Regex
const urls = [ "/v1/tron/instruments/PVCM/composition", "/v1/tron/instruments/Otro/composition", "/v1/tron/instruments/Serabe/composition", "/v1/tron/instruments/PVCM/compositon", ]; const results = ['PVCM', 'Otro', 'Serabe', false]; const regexp = new RegExp('\/v1\/tron\/instruments\/([^\]+)\/composition', "i") urls.forEach((url, idx) => { const match = url.match(regexp); if (typeof results[idx] === 'string') { if (match[1] !== results[idx]) { throw `Wrong #{match[1]} #{results[idx]}`; } } else if (results[idx] === false) { if (match !== null) { throw `Wrong, #{match} found`; } } });
Anchored Regex
const urls = [ "/v1/tron/instruments/PVCM/composition", "/v1/tron/instruments/Otro/composition", "/v1/tron/instruments/Serabe/composition", "/v1/tron/instruments/PVCM/compositon", ]; const results = ['PVCM', 'Otro', 'Serabe', false]; const regexp = new RegExp('^\/v1\/tron\/instruments\/([^\]+)\/composition$', "i") urls.forEach((url, idx) => { const match = url.match(regexp); if (typeof results[idx] === 'string') { if (match[1] !== results[idx]) { throw `Wrong #{match[1]} #{results[idx]}`; } } else if (results[idx] === false) { if (match !== null) { throw `Wrong, #{match} found`; } } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String handling
Simple Regex
Anchored 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):
I'll break down the benchmark and its test cases to help explain what's being tested. **Benchmark Overview** The benchmark is designed to measure the performance of JavaScript regex matching. The benchmark consists of three test cases: "String handling", "Simple Regex", and "Anchored Regex". Each test case uses a similar approach, but with slight variations in the regex pattern used. **Test Case 1: "String handling"** In this test case, the benchmark defines an array `urls` containing URLs with various formats. The script then iterates through the array, calling a function `extractSegment(url)` to extract a specific segment from each URL. If the extracted segment does not match the expected value in the `results` array, the script throws an error. The regex pattern used is empty (`const regexp = new RegExp('')`). This means that no regex matching is performed; instead, the script relies solely on string manipulation to extract the desired segment. **Pros and Cons:** * Pros: + Simple implementation + Does not rely on regex parsing * Cons: + May be slower due to manual string manipulation **Test Case 2: "Simple Regex"** In this test case, the benchmark uses a similar approach as Test Case 1, but with a regex pattern (`const regexp = new RegExp('\\/v1\\/tron\\/instruments\\/([^\\]+)\\/composition', 'i')`). This pattern matches URLs starting with `/v1/tron/instruments/` followed by any characters except `/`, and ending with `/composition`. **Pros and Cons:** * Pros: + Faster than manual string manipulation + Uses regex for pattern matching * Cons: + May be slower due to regex parsing **Test Case 3: "Anchored Regex"** In this test case, the benchmark uses an anchored regex pattern (`const regexp = new RegExp('^\\/v1\\/tron\\/instruments\\/([^\\]+)\\/composition$', 'i')`). This pattern matches URLs starting with `/v1/tron/instruments/` followed by any characters except `/`, and ending with `/composition`. **Pros and Cons:** * Pros: + Faster than non-anchored regex + Uses regex for pattern matching * Cons: + May be slower on older browsers that don't support anchored regex **Library Used:** None of the test cases use a specific library beyond the built-in JavaScript `RegExp` class. **Special JS Feature or Syntax:** The benchmark does not specifically target any special JS feature or syntax. It relies solely on standard JavaScript features and libraries (e.g., `RegExp`, string manipulation). **Alternatives:** There are alternative ways to optimize regex matching, such as: * Using a dedicated regex library like RegExp.js * Optimizing the regex pattern for specific use cases * Using a Just-In-Time (JIT) compiler or other performance optimization techniques However, these alternatives may require additional setup and expertise. Overall, the benchmark provides a good starting point to understand the performance characteristics of JavaScript regex matching.
Related benchmarks:
Simple Regex 3
regex vs startsWith vs indexOf perf on url
Check URL protocol and domain new URL, includes, endWith vs Regex
Partial base, search, fragment of URL
Comments
Confirm delete:
Do you really want to delete benchmark?