Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: fix setting url.search to the empty string #11105

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ Object.defineProperties(URL.prototype, {
if (!search) {
ctx.query = null;
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
this[searchParams][searchParams] = {};
this[searchParams][searchParams] = [];
Copy link
Member

Choose a reason for hiding this comment

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

Another way to do it is to not return here, create an else branch to wrap the stuff below and leave the initialization to initSearchParams(this[searchParams], search) later. That way URL doesn't need to know about the data sturcture of URLSearchParams

Copy link
Member Author

Choose a reason for hiding this comment

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

@joyeecheung, done, PTAL.

This does bring a drop to performance when !search. To counter this problem I added a few conditions to avoid parsing as much as possible.

After those changes, when alternative is changed to '', there is a 2.8% drop in performance; but when it is '?' there is a 400% increase, as measured by

node benchmark/compare.js --new ./after --old ./orig \
  --set n=1e5 --set prop=search --set 'url=http://example.com/' \
  --filter properties url

That's pretty good IMO.

return;
}
if (search[0] === '?') search = search.slice(1);
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-whatwg-url-searchparams.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ assert(sp.has('a'));
assert.strictEqual(sp.get('a'), '[object Object]');
sp.delete('a');
assert(!sp.has('a'));

m.search = '';
assert.strictEqual(sp.toString(), '');

values.forEach((i) => sp.append('a', i));
assert(sp.has('a'));
assert.strictEqual(sp.getAll('a').length, 6);
Expand Down