Skip to content

Commit

Permalink
url: improve invalid url performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 17, 2023
1 parent 7e12d0e commit 0501c68
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
23 changes: 23 additions & 0 deletions benchmark/url/whatwg-url-validity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common.js');
const url = require('url');
const URL = url.URL;

const bench = common.createBenchmark(main, {
type: ['valid', 'invalid'],
e: [1e5],
});

// This benchmark is used to compare the `Invalid URL` path of the URL parser
function main({ type, e }) {
const url = type === 'valid' ? 'https://www.nodejs.org' : 'www.nodejs.org';
bench.start();
for (let i = 0; i < e; i++) {
try {
new URL(url);
} catch {
// do nothing
}
}
bench.end(e);
}
8 changes: 1 addition & 7 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,7 @@ class URL {
base = `${base}`;
}

const href = bindingUrl.parse(input, base);

if (!href) {
throw new ERR_INVALID_URL(input);
}

this.#updateContext(href);
this.#updateContext(bindingUrl.parse(input, base));
}

[inspect.custom](depth, opts) {
Expand Down
4 changes: 2 additions & 2 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ void BindingData::Parse(const FunctionCallbackInfo<Value>& args) {
base =
ada::parse<ada::url_aggregator>(Utf8Value(isolate, args[1]).ToString());
if (!base) {
return args.GetReturnValue().Set(false);
return THROW_ERR_INVALID_URL(realm->env(), "Invalid URL");
}
base_pointer = &base.value();
}
auto out =
ada::parse<ada::url_aggregator>(input.ToStringView(), base_pointer);

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

binding_data->UpdateComponents(out->get_components(), out->type);
Expand Down

0 comments on commit 0501c68

Please sign in to comment.