Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions benchmark/url/url-searchparams-mutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
method: ['set', 'delete'],
type: ['unique', 'duplicates'],
count: [10, 1000],
n: [1e4],
});

function buildSeed(type, count) {
const parts = new Array(count);

if (type === 'duplicates') {
for (let i = 0; i < count; i++) {
parts[i] = `dup=${i}`;
}
} else {
for (let i = 0; i < count; i++) {
parts[i] = `k${i}=${i}`;
}
}

return new URLSearchParams(parts.join('&'));
}

function main({ method, type, count, n }) {
const seed = buildSeed(type, count);
const key = type === 'duplicates' ? 'dup' : 'k0';
const mutate = method === 'set' ?
(params) => params.set(key, 'updated') :
(params) => params.delete(key);

for (let i = 0; i < 1e3; i++) {
mutate(new URLSearchParams(seed));
}

bench.start();
for (let i = 0; i < n; i++) {
mutate(new URLSearchParams(seed));
}
bench.end(n);
}
49 changes: 35 additions & 14 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,26 +495,37 @@ class URLSearchParams {

const list = this.#searchParams;
name = StringPrototypeToWellFormed(`${name}`);
const len = list.length;
let write = 0;

if (value !== undefined) {
value = StringPrototypeToWellFormed(`${value}`);
for (let i = 0; i < list.length;) {
for (let i = 0; i < len; i += 2) {
if (list[i] === name && list[i + 1] === value) {
list.splice(i, 2);
} else {
i += 2;
continue;
}
if (write !== i) {
list[write] = list[i];
list[write + 1] = list[i + 1];
}
write += 2;
}
} else {
for (let i = 0; i < list.length;) {
for (let i = 0; i < len; i += 2) {
if (list[i] === name) {
list.splice(i, 2);
} else {
i += 2;
continue;
}
if (write !== i) {
list[write] = list[i];
list[write + 1] = list[i + 1];
}
write += 2;
}
}

if (write !== len)
list.length = write;

if (this.#context) {
setURLSearchParamsModified(this.#context);
}
Expand Down Expand Up @@ -594,24 +605,34 @@ class URLSearchParams {
const list = this.#searchParams;
name = StringPrototypeToWellFormed(`${name}`);
value = StringPrototypeToWellFormed(`${value}`);
const len = list.length;
Copy link
Copy Markdown
Contributor

@aduh95 aduh95 Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of declaring a variable we use once?

nit:

Suggested change
const len = list.length;
const { length } = list;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a habit of mine from the days when hoisting the length before for loops was faster

i can remove it if you'd like, but i applied your suggestion

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we do access it one more time later :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hoisting properties for a loop is usually not faster in V8


// If there are any name-value pairs whose name is `name`, in `list`, set
// the value of the first such name-value pair to `value` and remove the
// others.
let found = false;
for (let i = 0; i < list.length;) {
let write = 0;
for (let i = 0; i < len; i += 2) {
const cur = list[i];
let keep = true;
if (cur === name) {
if (!found) {
list[i + 1] = value;
list[write] = cur;
list[write + 1] = value;
found = true;
i += 2;
} else {
list.splice(i, 2);
keep = false;
}
} else {
i += 2;
} else if (write !== i) {
list[write] = cur;
list[write + 1] = list[i + 1];
}
if (keep)
write += 2;
}

if (found && write !== len) {
list.length = write;
}

// Otherwise, append a new name-value pair whose name is `name` and value
Expand Down
Loading