I am working with a large array of strings containing about 50,000 elements.
export const companies = [
"000014",
"000016",
"000017",
"000019",
"000020",
"000021",
"000023",
"000025",
...
]
These strings represent company names for which specific pages are displayed. I have implemented a middleware function that iterates through this array using a loop.
import { NextResponse, type NextRequest } from "next/server";
import { companies } from "./assets/companies";
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Loop to compare current URL path with company names
await for (let i = 0; i < companies.length; i++) {
if (pathname.startsWith(`/${companies[i]}`))
return NextResponse.redirect(new URL("/", req.url)); // Redirect to main page if company name matches current path
}
}
This process takes some time and I'm exploring ways to optimize it. One idea was to divide the array into chunks, but that may not be the most efficient solution.