diff --git a/benchmark/url/whatwgurl-canParse.js b/benchmark/url/whatwgurl-canParse.js new file mode 100644 index 00000000000000..65741d9884f106 --- /dev/null +++ b/benchmark/url/whatwgurl-canParse.js @@ -0,0 +1,14 @@ +'use strict'; +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + type: Object.keys(common.urls), + n: [25e6], +}); + +function main({ type, n }) { + bench.start(); + for (let i = 0; i < n; i += 1) + URL.canParse(common.urls[type]); + bench.end(n); +} diff --git a/doc/api/url.md b/doc/api/url.md index ab8b4154ff023e..de4a4cb85bce37 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -662,6 +662,27 @@ added: v16.7.0 Removes the stored {Blob} identified by the given ID. Attempting to revoke a ID that isn't registered will silently fail. +#### `URL.canParse(input[, base])` + + + +* `input` {string} The absolute or relative input URL to parse. If `input` + is relative, then `base` is required. If `input` is absolute, the `base` + is ignored. If `input` is not a string, it is [converted to a string][] first. +* `base` {string} The base URL to resolve against if the `input` is not + absolute. If `base` is not a string, it is [converted to a string][] first. +* Returns: {boolean} + +Checks if an `input` relative to the `base` can be parsed to a `URL`. + +```js +const isValid = URL.canParse('/foo', 'https://example.org/'); // true + +const isNotValid = URL.canParse('/foo'); // false +``` + ### Class: `URLSearchParams`