Skip to content

Commit 0501c68

Browse files
committed
url: improve invalid url performance
1 parent 7e12d0e commit 0501c68

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

benchmark/url/whatwg-url-validity.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const url = require('url');
4+
const URL = url.URL;
5+
6+
const bench = common.createBenchmark(main, {
7+
type: ['valid', 'invalid'],
8+
e: [1e5],
9+
});
10+
11+
// This benchmark is used to compare the `Invalid URL` path of the URL parser
12+
function main({ type, e }) {
13+
const url = type === 'valid' ? 'https://www.nodejs.org' : 'www.nodejs.org';
14+
bench.start();
15+
for (let i = 0; i < e; i++) {
16+
try {
17+
new URL(url);
18+
} catch {
19+
// do nothing
20+
}
21+
}
22+
bench.end(e);
23+
}

lib/internal/url.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,7 @@ class URL {
780780
base = `${base}`;
781781
}
782782

783-
const href = bindingUrl.parse(input, base);
784-
785-
if (!href) {
786-
throw new ERR_INVALID_URL(input);
787-
}
788-
789-
this.#updateContext(href);
783+
this.#updateContext(bindingUrl.parse(input, base));
790784
}
791785

792786
[inspect.custom](depth, opts) {

src/node_url.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
243243
base =
244244
ada::parse<ada::url_aggregator>(Utf8Value(isolate, args[1]).ToString());
245245
if (!base) {
246-
return args.GetReturnValue().Set(false);
246+
return THROW_ERR_INVALID_URL(realm->env(), "Invalid URL");
247247
}
248248
base_pointer = &base.value();
249249
}
250250
auto out =
251251
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);
252252

253253
if (!out) {
254-
return args.GetReturnValue().Set(false);
254+
return THROW_ERR_INVALID_URL(realm->env(), "Invalid URL");
255255
}
256256

257257
binding_data->UpdateComponents(out->get_components(), out->type);

0 commit comments

Comments
 (0)