Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
URL vs URLSearchParams vs RegExp
(version: 2)
Comparing performance of:
URL vs URLSearchParams vs RegExp1
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var location_href = "https://www.youtube.com/watch?v=x2rvSf0STBM&list=RDCLAK5uy_kvhjcPWzH7xZL-WnqGbiA_euQGy5_cbHI&start_radio=1"; var location_search = "?v=x2rvSf0STBM&list=RDCLAK5uy_kvhjcPWzH7xZL-WnqGbiA_euQGy5_cbHI&start_radio=1"; var loop = 10000; var loop_n = 3000; var urls = new Array(loop_n).fill(0).map(()=>{ let t = Math.round(Math.random()*80000+10000); return { href: `${location_href}${t}`, search: `${location_search}${t}` }; })
Tests:
URL
for(let i=loop_n;--i;){ new URL(urls[i].href).searchParams.has("list"); }
URLSearchParams
for(let i=loop_n;--i;){ new URLSearchParams(urls[i].search).has("list"); }
RegExp1
for(let i=loop_n;--i;){ /https\:\/\/www\.youtube\.com\/watch.*[\?\&]list=[\w\-\_]+/.test(urls[i].href) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
URL
URLSearchParams
RegExp1
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
URL
218.7 Ops/sec
URLSearchParams
277.6 Ops/sec
RegExp1
1127.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided benchmark compares three approaches to test if the value 'list' exists in a URL string: `new URL()` with `searchParams.has()`, `new URLSearchParams()` with `has()`, and a regular expression (`RegExp1`) that matches URLs with the 'list' parameter. **Test Cases** * **URL**: Creates a new `URL` object from the provided `href` attribute, which contains the URL string. Then, uses the `searchParams.has()` method to check if the value 'list' exists in the URL's search parameters. * **URLSearchParams**: Creates a new `URLSearchParams` object from the provided `search` attribute, which contains the URL string. Then, uses the `has()` method to check if the value 'list' exists in the URL's search parameters. * **RegExp1**: Uses a regular expression (`/https:\/\/www\.youtube\.com\/watch.*[\\?\\&]list=[\\w\\-\\_]+/`) to match URLs with the 'list' parameter. **Options Compared** The benchmark compares the performance of each approach: 1. `new URL()` with `searchParams.has()` 2. `new URLSearchParams()` with `has()` 3. Regular expression (`RegExp1`) that matches URLs with the 'list' parameter **Pros and Cons of Each Approach** * **`new URL()` with `searchParams.has()`**: + Pros: Easy to use, efficient, and widely supported. + Cons: May not work correctly for all URL formats, can be slower than other approaches. * **`new URLSearchParams()` with `has()`**: + Pros: More efficient than using a regular expression, works well for most URL formats. + Cons: Can be slower than the `URL` approach for certain use cases. * **Regular Expression (`RegExp1`)** + Pros: Highly customizable and can handle complex URL formats, but may be slower due to the overhead of regular expressions. **Library Usage** * None in this benchmark, as all three approaches rely on built-in JavaScript methods or regular expressions. **Special JS Features or Syntax** None mentioned in the provided code. **Other Alternatives** If you need to perform similar tasks, consider using other libraries or tools: 1. `url-parse`: A lightweight library for parsing URLs. 2. `query-string-escape`: A library for encoding and decoding URL query strings. 3. `regex`: A built-in JavaScript function for working with regular expressions. Keep in mind that these alternatives may offer more features, flexibility, or performance than the approaches compared in this benchmark.
Related benchmarks:
regex vs substring compare
URL to key/value parsing
JS Regex with OR (url) vs .startsWith
mybencmark11212224234
Comments
Confirm delete:
Do you really want to delete benchmark?