Skip to content

Commit 7eb52ec

Browse files
authoredMar 23, 2023
perf: replace endsWith with === (#12539)
1 parent eea1682 commit 7eb52ec

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed
 

‎packages/vite/src/node/server/middlewares/static.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function serveStaticMiddleware(
9292
// also skip internal requests `/@fs/ /@vite-client` etc...
9393
const cleanedUrl = cleanUrl(req.url!)
9494
if (
95-
cleanedUrl.endsWith('/') ||
95+
cleanedUrl[cleanedUrl.length - 1] === '/' ||
9696
path.extname(cleanedUrl) === '.html' ||
9797
isInternalRequest(req.url!)
9898
) {
@@ -123,7 +123,10 @@ export function serveStaticMiddleware(
123123

124124
const resolvedPathname = redirectedPathname || pathname
125125
let fileUrl = path.resolve(dir, removeLeadingSlash(resolvedPathname))
126-
if (resolvedPathname.endsWith('/') && !fileUrl.endsWith('/')) {
126+
if (
127+
resolvedPathname[resolvedPathname.length - 1] === '/' &&
128+
fileUrl[fileUrl.length - 1] !== '/'
129+
) {
127130
fileUrl = fileUrl + '/'
128131
}
129132
if (!ensureServingAccess(fileUrl, server, res, next)) {

‎packages/vite/src/node/utils.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export function fsPathFromUrl(url: string): string {
242242
* @returns true if dir is a parent of file
243243
*/
244244
export function isParentDirectory(dir: string, file: string): boolean {
245-
if (!dir.endsWith('/')) {
245+
if (dir[dir.length - 1] !== '/') {
246246
dir = `${dir}/`
247247
}
248248
return (
@@ -276,7 +276,7 @@ export const isJSRequest = (url: string): boolean => {
276276
if (knownJsSrcRE.test(url)) {
277277
return true
278278
}
279-
if (!path.extname(url) && !url.endsWith('/')) {
279+
if (!path.extname(url) && url[url.length - 1] !== '/') {
280280
return true
281281
}
282282
return false
@@ -292,7 +292,7 @@ const splitFilePathAndQueryRE = /(\.(?:[cm]?js|jsx))(\?.*)?$/
292292
export function getPotentialTsSrcPaths(filePath: string): string[] {
293293
const [name, type, query = ''] = filePath.split(splitFilePathAndQueryRE)
294294
const paths = [name + type.replace('js', 'ts') + query]
295-
if (!type.endsWith('x')) {
295+
if (type[type.length - 1] !== 'x') {
296296
paths.push(name + type.replace('js', 'tsx') + query)
297297
}
298298
return paths
@@ -1146,8 +1146,8 @@ function normalizeSingleAlias({
11461146
}: Alias): Alias {
11471147
if (
11481148
typeof find === 'string' &&
1149-
find.endsWith('/') &&
1150-
replacement.endsWith('/')
1149+
find[find.length - 1] === '/' &&
1150+
replacement[replacement.length - 1] === '/'
11511151
) {
11521152
find = find.slice(0, find.length - 1)
11531153
replacement = replacement.slice(0, replacement.length - 1)
@@ -1239,7 +1239,7 @@ export function joinUrlSegments(a: string, b: string): string {
12391239
if (!a || !b) {
12401240
return a || b || ''
12411241
}
1242-
if (a.endsWith('/')) {
1242+
if (a[a.length - 1] === '/') {
12431243
a = a.substring(0, a.length - 1)
12441244
}
12451245
if (b[0] !== '/') {
@@ -1256,7 +1256,7 @@ export function stripBase(path: string, base: string): string {
12561256
if (path === base) {
12571257
return '/'
12581258
}
1259-
const devBase = base.endsWith('/') ? base : base + '/'
1259+
const devBase = base[base.length - 1] === '/' ? base : base + '/'
12601260
return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path
12611261
}
12621262

0 commit comments

Comments
 (0)
Please sign in to comment.