Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getRelativeUrl
(version: 2)
Comparing performance of:
new URL vs RegExp vs getRelativeUrl
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function getRelativeUrl(url) { try { if (url.indexOf('http://') > -1) { return url.substring(url.indexOf('/', 7) + 1); } if (url.indexOf('https://') > -1) { return url.substring(url.indexOf('/', 8) + 1); } throw new Error('invalid url'); } catch (e) { console.error(e); return url; } };
Tests:
new URL
return new URL('https://is5-ssl.mzstatic.com/image/thumb/Purple126/v4/00/33/2e/00332e58-b196-82cf-1231-82d81d6fe477/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/512x0w.png').origin;
RegExp
return 'https://is5-ssl.mzstatic.com/image/thumb/Purple126/v4/00/33/2e/00332e58-b196-82cf-1231-82d81d6fe477/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/512x0w.png'.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)(\/[^?#]*)?(\?[^#]*)?/)[0]
getRelativeUrl
return getRelativeUrl('https://is5-ssl.mzstatic.com/image/thumb/Purple126/v4/00/33/2e/00332e58-b196-82cf-1231-82d81d6fe477/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/512x0w.png')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
new URL
RegExp
getRelativeUrl
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new URL
516661.4 Ops/sec
RegExp
2104206.5 Ops/sec
getRelativeUrl
10841757.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript benchmark test case, specifically designed to measure the performance of three different approaches for extracting a relative URL from a given string. **Benchmark Definition** The `getRelativeUrl` function is defined in the `Script Preparation Code` section. It takes a URL as an input and returns the relative URL if it's already present within the original URL. If not, it throws an error or returns the original URL (depending on whether an exception was caught). **Options Compared** There are three options being compared: 1. **new URL**: A built-in JavaScript API that provides a `URL` class for parsing and manipulating URLs. 2. **RegExp**: A regular expression object used to match patterns in strings, used to extract the relative URL from the input string. 3. **Custom implementation (getRelativeUrl)**: The original function defined in the benchmark preparation code. **Pros and Cons** * **new URL**: Pros: * Built-in API, no additional library dependency * Robust parsing of URLs with various schemes, hosts, paths, and query parameters Cons: * Not as optimized for performance compared to custom implementations * **RegExp**: Pros: * Lightweight and efficient, suitable for simple URL extraction tasks Cons: * Requires manual configuration of regular expression patterns * May not handle edge cases or complex URLs effectively * **Custom implementation (getRelativeUrl)**: Pros: * Can be optimized for performance, depending on the specific requirements * Allows direct manipulation of URL components **Library and Purpose** The `URL` class is a built-in JavaScript API used to manipulate URLs. Its purpose is to provide a standardized way to work with URLs in JavaScript, including parsing, constructing, and manipulating them. **Special JS Feature or Syntax** This benchmark does not specifically test any special JavaScript features or syntax beyond the standard `URL` class. However, it does rely on the browser's implementation of the `URL` class, which may vary between different browsers and versions. **Alternatives** Other alternatives for URL manipulation in JavaScript include: * The `URI` class (older API) * Third-party libraries like `url-parse`, `urijs`, or `url-intern` * Regular expressions with custom implementations When choosing an alternative, consider factors such as performance requirements, ease of use, and compatibility across different browsers and versions. **Benchmark Preparation Code** The provided code is a simple JavaScript function that extracts the relative URL from the input string. It uses regular expressions to match the scheme, host, path, and query parameters. ```javascript function getRelativeUrl(url) { try { if (url.indexOf('http://') > -1) { return url.substring(url.indexOf('/', 7) + 1); } if (url.indexOf('https://') > -1) { return url.substring(url.indexOf('/', 8) + 1); } throw new Error('invalid url'); } catch (e) { console.error(e); return url; } } ``` The benchmark definition uses this function to test the performance of different approaches for extracting a relative URL from a given string.
Related benchmarks:
Check URL protocol and domain new URL, includes, endWith vs Regex
Partial base, search, fragment of URL 2
Partial base, search, fragment of URL 3
Regex vs URL
Comments
Confirm delete:
Do you really want to delete benchmark?