Skip to content

Commit

Permalink
url: offload URLSearchParams initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Feb 27, 2023
1 parent 69148f0 commit dc80660
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const FORWARD_SLASH = /\//g;

const context = Symbol('context');
const searchParams = Symbol('query');
const kDirty = Symbol('dirty');

const updateActions = {
kProtocol: 0,
Expand Down Expand Up @@ -224,11 +225,12 @@ class URLSearchParams {
} else {
// USVString
init = toUSVString(init);
initSearchParams(this, init);
this[searchParams] = init ? parseParams(init) : [];
}

// "associated url object"
this[context] = null;
this[kDirty] = false;
}

[inspect.custom](recurseTimes, ctx) {
Expand Down Expand Up @@ -545,6 +547,7 @@ class URL {
input = `${input}`;
this[context] = new URLContext();
this.#onParseComplete = FunctionPrototypeBind(this.#onParseComplete, this);
this.#onSearchUpdate = FunctionPrototypeBind(this.#onSearchUpdate, this);

if (base !== undefined) {
base = `${base}`;
Expand Down Expand Up @@ -604,11 +607,34 @@ class URL {
ctx.password = password;
ctx.port = port;
ctx.hash = hash;
if (!this[searchParams]) { // Invoked from URL constructor
this[searchParams] = new URLSearchParams();
if (this[searchParams]) {
// Update `kDirty` property to recalculate searchParams on access.
// This is done to reduce the overhead of initializing the URL.
this[searchParams][kDirty] = true;
}
};

#onSearchUpdate = (href, origin, protocol, hostname, pathname,
search, username, password, port, hash) => {
const ctx = this[context];
ctx.href = href;
ctx.origin = origin;
ctx.protocol = protocol;
ctx.hostname = hostname;
ctx.pathname = pathname;
ctx.search = search;
ctx.username = username;
ctx.password = password;
ctx.port = port;
ctx.hash = hash;

if (this[searchParams] == null) {
this[searchParams] = new URLSearchParams(this[context].search);
this[searchParams][context] = this;
} else {
this[searchParams][searchParams] = this[context].search ? parseParams(this[context].search) : [];
this[searchParams][kDirty] = false;
}
initSearchParams(this[searchParams], ctx.search);
};

toString() {
Expand Down Expand Up @@ -729,18 +755,25 @@ class URL {
return this[context].search;
}

set search(search) {
set search(value) {
if (!isURLThis(this))
throw new ERR_INVALID_THIS('URL');
search = toUSVString(search);
updateUrl(this[context].href, updateActions.kSearch, search, this.#onParseComplete);
initSearchParams(this[searchParams], this[context].search);
updateUrl(this[context].href, updateActions.kSearch, toUSVString(value), this.#onSearchUpdate);
}

// readonly
get searchParams() {
if (!isURLThis(this))
throw new ERR_INVALID_THIS('URL');
// Create URLSearchParams on demand to greatly improve the URL performance.
if (this[searchParams] == null) {
this[searchParams] = new URLSearchParams(this[context].search);
this[searchParams][context] = this;
} else if (this[searchParams][kDirty]) {
const updated = this[context].search;
this[searchParams][searchParams] = updated ? parseParams(updated) : [];
this[searchParams][kDirty] = false;
}
return this[searchParams];
}

Expand Down Expand Up @@ -815,14 +848,6 @@ ObjectDefineProperties(URL, {
revokeObjectURL: kEnumerableProperty,
});

function initSearchParams(url, init) {
if (!init) {
url[searchParams] = [];
return;
}
url[searchParams] = parseParams(init);
}

// application/x-www-form-urlencoded parser
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-parser
function parseParams(qs) {
Expand Down

0 comments on commit dc80660

Please sign in to comment.