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

perf: replace endsWith with === #12539

Merged
merged 5 commits into from Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -92,7 +92,7 @@ export function serveStaticMiddleware(
// also skip internal requests `/@fs/ /@vite-client` etc...
const cleanedUrl = cleanUrl(req.url!)
if (
cleanedUrl.endsWith('/') ||
cleanedUrl[cleanedUrl.length - 1] === '/' ||
path.extname(cleanedUrl) === '.html' ||
isInternalRequest(req.url!)
) {
Expand Down Expand Up @@ -123,7 +123,10 @@ export function serveStaticMiddleware(

const resolvedPathname = redirectedPathname || pathname
let fileUrl = path.resolve(dir, removeLeadingSlash(resolvedPathname))
if (resolvedPathname.endsWith('/') && !fileUrl.endsWith('/')) {
if (
resolvedPathname[resolvedPathname.length - 1] === '/' &&
fileUrl[fileUrl.length - 1] !== '/'
) {
fileUrl = fileUrl + '/'
}
if (!ensureServingAccess(fileUrl, server, res, next)) {
Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/node/utils.ts
Expand Up @@ -242,7 +242,7 @@ export function fsPathFromUrl(url: string): string {
* @returns true if dir is a parent of file
*/
export function isParentDirectory(dir: string, file: string): boolean {
if (!dir.endsWith('/')) {
if (dir[dir.length - 1] !== '/') {
dir = `${dir}/`
}
return (
Expand Down Expand Up @@ -276,7 +276,7 @@ export const isJSRequest = (url: string): boolean => {
if (knownJsSrcRE.test(url)) {
return true
}
if (!path.extname(url) && !url.endsWith('/')) {
if (!path.extname(url) && url[url.length - 1] !== '/') {
return true
}
return false
Expand All @@ -292,7 +292,7 @@ const splitFilePathAndQueryRE = /(\.(?:[cm]?js|jsx))(\?.*)?$/
export function getPotentialTsSrcPaths(filePath: string): string[] {
const [name, type, query = ''] = filePath.split(splitFilePathAndQueryRE)
const paths = [name + type.replace('js', 'ts') + query]
if (!type.endsWith('x')) {
if (type[type.length - 1] !== 'x') {
paths.push(name + type.replace('js', 'tsx') + query)
}
return paths
Expand Down Expand Up @@ -1146,8 +1146,8 @@ function normalizeSingleAlias({
}: Alias): Alias {
if (
typeof find === 'string' &&
find.endsWith('/') &&
replacement.endsWith('/')
find[find.length - 1] === '/' &&
replacement[replacement.length - 1] === '/'
) {
find = find.slice(0, find.length - 1)
replacement = replacement.slice(0, replacement.length - 1)
Expand Down Expand Up @@ -1239,7 +1239,7 @@ export function joinUrlSegments(a: string, b: string): string {
if (!a || !b) {
return a || b || ''
}
if (a.endsWith('/')) {
if (a[a.length - 1] === '/') {
a = a.substring(0, a.length - 1)
}
if (b[0] !== '/') {
Expand All @@ -1256,7 +1256,7 @@ export function stripBase(path: string, base: string): string {
if (path === base) {
return '/'
}
const devBase = base.endsWith('/') ? base : base + '/'
const devBase = base[base.length - 1] === '/' ? base : base + '/'
return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path
}

Expand Down