-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
url: improve URLSearchParams creation performance #47190
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
Merged
nodejs-github-bot
merged 1 commit into
nodejs:main
from
anonrig:url-search-params-improve
Apr 3, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const { | ||
Array, | ||
ArrayIsArray, | ||
ArrayPrototypeJoin, | ||
ArrayPrototypeMap, | ||
ArrayPrototypePush, | ||
|
@@ -151,52 +152,74 @@ function isURLSearchParams(self) { | |
} | ||
|
||
class URLSearchParams { | ||
[searchParams] = []; | ||
|
||
// "associated url object" | ||
[context] = null; | ||
|
||
// URL Standard says the default value is '', but as undefined and '' have | ||
// the same result, undefined is used to prevent unnecessary parsing. | ||
// Default parameter is necessary to keep URLSearchParams.length === 0 in | ||
// accordance with Web IDL spec. | ||
constructor(init = undefined) { | ||
if (init === null || init === undefined) { | ||
this[searchParams] = []; | ||
if (init == null) { | ||
// Do nothing | ||
} else if (typeof init === 'object' || typeof init === 'function') { | ||
const method = init[SymbolIterator]; | ||
if (method === this[SymbolIterator]) { | ||
// While the spec does not have this branch, we can use it as a | ||
// shortcut to avoid having to go through the costly generic iterator. | ||
const childParams = init[searchParams]; | ||
this[searchParams] = childParams.slice(); | ||
} else if (method !== null && method !== undefined) { | ||
} else if (method != null) { | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Sequence<sequence<USVString>> | ||
if (typeof method !== 'function') { | ||
throw new ERR_ARG_NOT_ITERABLE('Query pairs'); | ||
} | ||
|
||
// Sequence<sequence<USVString>> | ||
// Note: per spec we have to first exhaust the lists then process them | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const pairs = []; | ||
// The following implementationd differs from the URL specification: | ||
// Sequences must first be converted from ECMAScript objects before | ||
// and operations are done on them, and the operation of converting | ||
// the sequences would first exhaust the iterators. If the iterator | ||
// returns something invalid in the middle, whether it would be called | ||
// after that would be an observable change to the users. | ||
// Exhausting the iterator and later converting them to USVString comes | ||
// with a significant cost (~40-80%). In order optimize URLSearchParams | ||
// creation duration, Node.js merges the iteration and converting | ||
// iterations into a single iteration. | ||
for (const pair of init) { | ||
if ((typeof pair !== 'object' && typeof pair !== 'function') || | ||
pair === null || | ||
typeof pair[SymbolIterator] !== 'function') { | ||
if (pair == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this branch to be with the catch-all if (pair == null ||
typeof pair !== 'object' && typeof pair !== 'function' ||
typeof pair[SymbolIterator] !== 'function')) { but it reads a bit nicer. |
||
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); | ||
} | ||
const convertedPair = []; | ||
for (const element of pair) | ||
ArrayPrototypePush(convertedPair, toUSVString(element)); | ||
ArrayPrototypePush(pairs, convertedPair); | ||
} | ||
} else if (ArrayIsArray(pair)) { | ||
// If innerSequence's size is not 2, then throw a TypeError. | ||
if (pair.length !== 2) { | ||
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); | ||
} | ||
// Append (innerSequence[0], innerSequence[1]) to querys list. | ||
ArrayPrototypePush(this[searchParams], toUSVString(pair[0]), toUSVString(pair[1])); | ||
} else { | ||
if (((typeof pair !== 'object' && typeof pair !== 'function') || | ||
typeof pair[SymbolIterator] !== 'function')) { | ||
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); | ||
} | ||
|
||
this[searchParams] = []; | ||
for (const pair of pairs) { | ||
if (pair.length !== 2) { | ||
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); | ||
let length = 0; | ||
|
||
for (const element of pair) { | ||
length++; | ||
ArrayPrototypePush(this[searchParams], toUSVString(element)); | ||
} | ||
|
||
// If innerSequence's size is not 2, then throw a TypeError. | ||
if (length !== 2) { | ||
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); | ||
} | ||
} | ||
ArrayPrototypePush(this[searchParams], pair[0], pair[1]); | ||
} | ||
} else { | ||
// Record<USVString, USVString> | ||
// Need to use reflection APIs for full spec compliance. | ||
const visited = {}; | ||
this[searchParams] = []; | ||
const keys = ReflectOwnKeys(init); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
|
@@ -218,13 +241,10 @@ class URLSearchParams { | |
} | ||
} | ||
} else { | ||
// USVString | ||
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams | ||
init = toUSVString(init); | ||
this[searchParams] = init ? parseParams(init) : []; | ||
} | ||
|
||
// "associated url object" | ||
this[context] = null; | ||
} | ||
|
||
[inspect.custom](recurseTimes, ctx) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.