Skip to content

Commit 827fb90

Browse files
authored
chore: break calls to forEach into for loops (vercel#74523)
This breaks a few functions down that were using array methods before to use for loops instead. Reducing the amount of functions created should improve performance a tiny bit. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 286c14d commit 827fb90

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

packages/next/src/server/base-server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,18 +1160,18 @@ export default abstract class Server<
11601160
if (didRewrite && parsedUrl.pathname) {
11611161
addRequestMeta(req, 'rewroteURL', parsedUrl.pathname)
11621162
}
1163-
const routeParamKeys = new Set<string>()
1164-
1165-
for (const key of Object.keys(parsedUrl.query)) {
1166-
const value = parsedUrl.query[key]
11671163

1168-
normalizeNextQueryParam(key, (normalizedKey) => {
1169-
if (!parsedUrl) return // typeguard
1164+
// Normalize all the query params to remove the prefixes.
1165+
const routeParamKeys = new Set<string>()
1166+
for (const [key, value] of Object.entries(parsedUrl.query)) {
1167+
if (typeof value === 'undefined') continue
11701168

1169+
const normalizedKey = normalizeNextQueryParam(key)
1170+
if (normalizedKey) {
11711171
parsedUrl.query[normalizedKey] = value
11721172
routeParamKeys.add(normalizedKey)
11731173
delete parsedUrl.query[key]
1174-
})
1174+
}
11751175
}
11761176

11771177
// interpolate dynamic params and normalize URL if needed

packages/next/src/server/web/adapter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ export async function adapter(
110110
for (const key of keys) {
111111
const value = requestUrl.searchParams.getAll(key)
112112

113-
normalizeNextQueryParam(key, (normalizedKey) => {
113+
const normalizedKey = normalizeNextQueryParam(key)
114+
if (normalizedKey) {
114115
requestUrl.searchParams.delete(normalizedKey)
115-
116116
for (const val of value) {
117117
requestUrl.searchParams.append(normalizedKey, val)
118118
}
119119
requestUrl.searchParams.delete(key)
120-
})
120+
}
121121
}
122122

123123
// Ensure users only see page requests, never data requests.

packages/next/src/server/web/utils.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,15 @@ export function validateURL(url: string | URL): string {
153153

154154
/**
155155
* Normalizes `nxtP` and `nxtI` query param values to remove the prefix.
156-
* This function does not mutate the input key; it calls the provided function
157-
* with the normalized key.
156+
* This function does not mutate the input key.
158157
*/
159-
export function normalizeNextQueryParam(
160-
key: string,
161-
onKeyNormalized: (normalizedKey: string) => void
162-
) {
158+
export function normalizeNextQueryParam(key: string): null | string {
163159
const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX]
164160
for (const prefix of prefixes) {
165161
if (key !== prefix && key.startsWith(prefix)) {
166162
const normalizedKey = key.substring(prefix.length)
167-
onKeyNormalized(normalizedKey)
163+
return normalizedKey
168164
}
169165
}
166+
return null
170167
}

packages/next/src/shared/lib/router/utils/querystring.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ export function searchParamsToUrlQuery(
44
searchParams: URLSearchParams
55
): ParsedUrlQuery {
66
const query: ParsedUrlQuery = {}
7-
searchParams.forEach((value, key) => {
8-
if (typeof query[key] === 'undefined') {
7+
for (const [key, value] of searchParams.entries()) {
8+
const existing = query[key]
9+
if (typeof existing === 'undefined') {
910
query[key] = value
10-
} else if (Array.isArray(query[key])) {
11-
;(query[key] as string[]).push(value)
11+
} else if (Array.isArray(existing)) {
12+
existing.push(value)
1213
} else {
13-
query[key] = [query[key] as string, value]
14+
query[key] = [existing, value]
1415
}
15-
})
16+
}
1617
return query
1718
}
1819

1920
function stringifyUrlQueryParam(param: unknown): string {
21+
if (typeof param === 'string') {
22+
return param
23+
}
24+
2025
if (
21-
typeof param === 'string' ||
2226
(typeof param === 'number' && !isNaN(param)) ||
2327
typeof param === 'boolean'
2428
) {
@@ -28,27 +32,33 @@ function stringifyUrlQueryParam(param: unknown): string {
2832
}
2933
}
3034

31-
export function urlQueryToSearchParams(
32-
urlQuery: ParsedUrlQuery
33-
): URLSearchParams {
34-
const result = new URLSearchParams()
35-
Object.entries(urlQuery).forEach(([key, value]) => {
35+
export function urlQueryToSearchParams(query: ParsedUrlQuery): URLSearchParams {
36+
const searchParams = new URLSearchParams()
37+
for (const [key, value] of Object.entries(query)) {
3638
if (Array.isArray(value)) {
37-
value.forEach((item) => result.append(key, stringifyUrlQueryParam(item)))
39+
for (const item of value) {
40+
searchParams.append(key, stringifyUrlQueryParam(item))
41+
}
3842
} else {
39-
result.set(key, stringifyUrlQueryParam(value))
43+
searchParams.set(key, stringifyUrlQueryParam(value))
4044
}
41-
})
42-
return result
45+
}
46+
return searchParams
4347
}
4448

4549
export function assign(
4650
target: URLSearchParams,
4751
...searchParamsList: URLSearchParams[]
4852
): URLSearchParams {
49-
searchParamsList.forEach((searchParams) => {
50-
Array.from(searchParams.keys()).forEach((key) => target.delete(key))
51-
searchParams.forEach((value, key) => target.append(key, value))
52-
})
53+
for (const searchParams of searchParamsList) {
54+
for (const key of searchParams.keys()) {
55+
target.delete(key)
56+
}
57+
58+
for (const [key, value] of searchParams.entries()) {
59+
target.append(key, value)
60+
}
61+
}
62+
5363
return target
5464
}

0 commit comments

Comments
 (0)