Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Simple Regex 3
(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):
**Benchmark Overview** The provided benchmark is designed to test the performance of JavaScript in handling strings, specifically regular expressions. The benchmark consists of three test cases: `String Handling`, `Simple Regex`, and `Anchored Regex`. Each test case uses a similar approach but with different variations. **Test Case 1: String Handling** This test case uses a custom function `extractSegment` to extract a segment from a URL string. The function checks if the extracted segment matches a predefined list of expected values. The benchmark verifies that each extracted segment corresponds to an expected value in the list. The pros of this approach are: * It allows for fine-grained control over the input data and the verification logic. * It is specific to the use case being tested. The cons are: * It may be slower due to the custom function implementation. * The benchmark requires a large number of iterations to produce reliable results. **Test Case 2: Simple Regex** This test case uses a regular expression to match a URL pattern. The regex pattern matches the `^` character followed by `/v1/tron/instruments/` and then captures one or more non-slash characters (`[^/]`). The benchmark verifies that the matched string corresponds to an expected value in the list. The pros of this approach are: * It is generally faster than a custom implementation. * It can handle large inputs efficiently. The cons are: * The regex pattern may be less specific than a custom implementation. * The benchmark requires careful tuning of the regex pattern to produce reliable results. **Test Case 3: Anchored Regex** This test case uses an anchored regular expression to match a URL pattern. The regex pattern matches the `^` character followed by `/v1/tron/instruments/`, captures one or more non-slash characters (`[^/]`), and then checks if the matched string is equal to a specific value. The pros of this approach are: * It provides better specificity than a simple regex. * The benchmark can be optimized for performance. The cons are: * The regex pattern may be less flexible than a custom implementation. * The benchmark requires careful tuning of the regex pattern and expected values. **Library: None** There is no explicit library used in these test cases. However, Safari's JavaScript engine might have some internal optimizations or features that could impact the performance of the benchmarks. **Special JS Features/Syntax: None** None of the test cases use special JavaScript features or syntax, such as async/await, Promises, or WebAssembly. **Alternative Approaches** Other alternatives to these approaches include: 1. Using a third-party library like RegEx or regex-test for regular expression matching. 2. Implementing a custom string extraction function using JavaScript's built-in string manipulation methods (e.g., `split()`, `substring()`). 3. Using a benchmarking framework like Benchmark.js or Benchmark-Test to simplify the benchmarking process. It is worth noting that these alternatives might not provide significant performance improvements over the current implementation and may introduce additional complexity.
Related benchmarks:
Simple Regex 2
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?