Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
URLSearchParams.has vs URLSearchParams.get with undefined returning
(version: 0)
Comparing performance of:
URL vs URLSearchParams
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var param = "?id=4&benchmark=5832"; var url = "https://www.measurethat.net" + param; var loop = 10000; var params = new URLSearchParams(param);
Tests:
URL
params.has('benchmark') ? params.get('benchmark') : undefined;
URLSearchParams
params.get('benchmark') ?? undefined;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
URL
URLSearchParams
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
URL
3238255.0 Ops/sec
URLSearchParams
6116610.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance difference between two approaches to retrieve a parameter value from `URLSearchParams`: `has` vs `get`. The `has` method returns `true` if a key exists in the search parameters, while the `get` method returns the value associated with a given key or `undefined` if not found. **Options Compared** Two options are compared: 1. **`params.has('benchmark') ? params.get('benchmark') : undefined;`**: This approach uses the ternary operator to first check if the `benchmark` parameter exists using `has`, and then returns its value using `get`. If it doesn't exist, it returns `undefined`. 2. **`params.get('benchmark') ?? undefined;`**: This approach uses the nullish coalescing operator (`??`) to return the value of `benchmark` if it exists in the search parameters, or `undefined` if not. **Pros and Cons** 1. **`has` + `get` (Ternary Operator)**: * Pros: More explicit and readable code. * Cons: Requires an additional comparison using `==`, which can lead to performance overhead. 2. **`get` with nullish coalescing operator (`??`)**: * Pros: More concise and efficient code, as the `get` method already returns `undefined` if not found. * Cons: May be less readable for developers unfamiliar with the nullish coalescing operator. **Library** In this benchmark, `URLSearchParams` is used, which is a built-in JavaScript API for parsing URL query strings. **Special JS Feature/Syntax** The nullish coalescing operator (`??`) was introduced in ECMAScript 2020 (ES12) and provides a concise way to handle cases where a value might be null or undefined. It's not widely supported by older browsers, so this benchmark may have performance differences depending on the browser version. **Other Considerations** * The `loop` variable is set to 10,000, which controls the number of executions for each test case. * The `params` object is created using the `URLSearchParams` constructor, and its search parameters are populated with a sample URL string. * The benchmark results are reported as raw UA strings (e.g., "Mozilla/5.0..."), browser versions, device platforms, operating systems, executions per second, and test names. **Alternatives** If you were to rewrite this benchmark using different approaches, some alternatives could be: 1. Using a third-party library like `url-search-params` or `search-params`. 2. Implementing the `has` and `get` methods yourself for educational purposes. 3. Comparing other alternative syntaxes, such as Python's `dict.get()` method. However, using built-in APIs like `URLSearchParams` is usually the most efficient and straightforward way to parse URL query strings in JavaScript.
Related benchmarks:
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty
undefined vs. typeof vs. in vs. hasOwnProperty vs bool
undefined vs. typeof vs. in vs. hasOwnProperty 23
return : undefined vs. typeof vs. in vs. hasOwnProperty vs try catch
Comments
Confirm delete:
Do you really want to delete benchmark?