<!--your preparation HTML code goes here-->
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}`
};
})
for(let i=loop_n;--i;){
new URL(urls[i].href).searchParams.delete("start_radio");
}
for(let i=loop_n;--i;){
const regex = new RegExp('[?|&]start_radio=\\d', '');
urls[i].href.replace(regex, '')
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Deletion with Url constructor and search params | |
Deletion with RegExp and replace |
Test name | Executions per second |
---|---|
Deletion with Url constructor and search params | 143.5 Ops/sec |
Deletion with RegExp and replace | 3467.1 Ops/sec |
The benchmark described compares the performance of two different methods for deleting the query parameter start_radio
from a URL.
Using the URL Constructor and searchParams.delete:
for(let i=loop_n;--i;){
new URL(urls[i].href).searchParams.delete("start_radio");
}
URL
object for each URL string and utilizes the built-in searchParams
API to remove the start_radio
parameter.Using Regular Expressions with String.replace:
for(let i=loop_n;--i;){
const regex = new RegExp('[?|&]start_radio=\\d', '');
urls[i].href.replace(regex, '');
}
start_radio
parameter in the query string and replaces it with an empty string to effectively remove it.URL Constructor and searchParams.delete:
Regular Expressions with String.replace:
URL
interface and searchParams
API are modern JavaScript features that adhere to the URL specification, and they provide a clean, structured way to deal with URLs.URLSearchParams
API directly to enhance performance while avoiding the overhead of creating URL objects.query-string
or qs
can provide utility functions to manipulate query strings flexibly and safely, with potentially better performance in batch scenarios.In conclusion, the choice between these methods should consider the trade-offs between performance and code clarity, especially given the context in which they'll be used.