Skip to content

Commit

Permalink
perf: replace endsWith with === (#12539)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Mar 23, 2023
1 parent eea1682 commit 7eb52ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
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

0 comments on commit 7eb52ec

Please sign in to comment.