Unverified Commit 827fb90b authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

chore: break calls to forEach into for loops (#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 #

-->
parent 286c14d0
...@@ -1160,18 +1160,18 @@ export default abstract class Server< ...@@ -1160,18 +1160,18 @@ export default abstract class Server<
if (didRewrite && parsedUrl.pathname) { if (didRewrite && parsedUrl.pathname) {
addRequestMeta(req, 'rewroteURL', parsedUrl.pathname) addRequestMeta(req, 'rewroteURL', parsedUrl.pathname)
} }
const routeParamKeys = new Set<string>()
for (const key of Object.keys(parsedUrl.query)) {
const value = parsedUrl.query[key]
normalizeNextQueryParam(key, (normalizedKey) => { // Normalize all the query params to remove the prefixes.
if (!parsedUrl) return // typeguard const routeParamKeys = new Set<string>()
for (const [key, value] of Object.entries(parsedUrl.query)) {
if (typeof value === 'undefined') continue
const normalizedKey = normalizeNextQueryParam(key)
if (normalizedKey) {
parsedUrl.query[normalizedKey] = value parsedUrl.query[normalizedKey] = value
routeParamKeys.add(normalizedKey) routeParamKeys.add(normalizedKey)
delete parsedUrl.query[key] delete parsedUrl.query[key]
}) }
} }
// interpolate dynamic params and normalize URL if needed // interpolate dynamic params and normalize URL if needed
......
...@@ -110,14 +110,14 @@ export async function adapter( ...@@ -110,14 +110,14 @@ export async function adapter(
for (const key of keys) { for (const key of keys) {
const value = requestUrl.searchParams.getAll(key) const value = requestUrl.searchParams.getAll(key)
normalizeNextQueryParam(key, (normalizedKey) => { const normalizedKey = normalizeNextQueryParam(key)
if (normalizedKey) {
requestUrl.searchParams.delete(normalizedKey) requestUrl.searchParams.delete(normalizedKey)
for (const val of value) { for (const val of value) {
requestUrl.searchParams.append(normalizedKey, val) requestUrl.searchParams.append(normalizedKey, val)
} }
requestUrl.searchParams.delete(key) requestUrl.searchParams.delete(key)
}) }
} }
// Ensure users only see page requests, never data requests. // Ensure users only see page requests, never data requests.
......
...@@ -153,18 +153,15 @@ export function validateURL(url: string | URL): string { ...@@ -153,18 +153,15 @@ export function validateURL(url: string | URL): string {
/** /**
* Normalizes `nxtP` and `nxtI` query param values to remove the prefix. * Normalizes `nxtP` and `nxtI` query param values to remove the prefix.
* This function does not mutate the input key; it calls the provided function * This function does not mutate the input key.
* with the normalized key.
*/ */
export function normalizeNextQueryParam( export function normalizeNextQueryParam(key: string): null | string {
key: string,
onKeyNormalized: (normalizedKey: string) => void
) {
const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX] const prefixes = [NEXT_QUERY_PARAM_PREFIX, NEXT_INTERCEPTION_MARKER_PREFIX]
for (const prefix of prefixes) { for (const prefix of prefixes) {
if (key !== prefix && key.startsWith(prefix)) { if (key !== prefix && key.startsWith(prefix)) {
const normalizedKey = key.substring(prefix.length) const normalizedKey = key.substring(prefix.length)
onKeyNormalized(normalizedKey) return normalizedKey
} }
} }
return null
} }
...@@ -4,21 +4,25 @@ export function searchParamsToUrlQuery( ...@@ -4,21 +4,25 @@ export function searchParamsToUrlQuery(
searchParams: URLSearchParams searchParams: URLSearchParams
): ParsedUrlQuery { ): ParsedUrlQuery {
const query: ParsedUrlQuery = {} const query: ParsedUrlQuery = {}
searchParams.forEach((value, key) => { for (const [key, value] of searchParams.entries()) {
if (typeof query[key] === 'undefined') { const existing = query[key]
if (typeof existing === 'undefined') {
query[key] = value query[key] = value
} else if (Array.isArray(query[key])) { } else if (Array.isArray(existing)) {
;(query[key] as string[]).push(value) existing.push(value)
} else { } else {
query[key] = [query[key] as string, value] query[key] = [existing, value]
} }
}) }
return query return query
} }
function stringifyUrlQueryParam(param: unknown): string { function stringifyUrlQueryParam(param: unknown): string {
if (typeof param === 'string') {
return param
}
if ( if (
typeof param === 'string' ||
(typeof param === 'number' && !isNaN(param)) || (typeof param === 'number' && !isNaN(param)) ||
typeof param === 'boolean' typeof param === 'boolean'
) { ) {
...@@ -28,27 +32,33 @@ function stringifyUrlQueryParam(param: unknown): string { ...@@ -28,27 +32,33 @@ function stringifyUrlQueryParam(param: unknown): string {
} }
} }
export function urlQueryToSearchParams( export function urlQueryToSearchParams(query: ParsedUrlQuery): URLSearchParams {
urlQuery: ParsedUrlQuery const searchParams = new URLSearchParams()
): URLSearchParams { for (const [key, value] of Object.entries(query)) {
const result = new URLSearchParams()
Object.entries(urlQuery).forEach(([key, value]) => {
if (Array.isArray(value)) { if (Array.isArray(value)) {
value.forEach((item) => result.append(key, stringifyUrlQueryParam(item))) for (const item of value) {
searchParams.append(key, stringifyUrlQueryParam(item))
}
} else { } else {
result.set(key, stringifyUrlQueryParam(value)) searchParams.set(key, stringifyUrlQueryParam(value))
} }
}) }
return result return searchParams
} }
export function assign( export function assign(
target: URLSearchParams, target: URLSearchParams,
...searchParamsList: URLSearchParams[] ...searchParamsList: URLSearchParams[]
): URLSearchParams { ): URLSearchParams {
searchParamsList.forEach((searchParams) => { for (const searchParams of searchParamsList) {
Array.from(searchParams.keys()).forEach((key) => target.delete(key)) for (const key of searchParams.keys()) {
searchParams.forEach((value, key) => target.append(key, value)) target.delete(key)
}) }
for (const [key, value] of searchParams.entries()) {
target.append(key, value)
}
}
return target return target
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment