Skip to content

Commit c42e64e

Browse files
authored
perf: replace startsWith with strict equality (#8546)
1 parent d7fca03 commit c42e64e

File tree

14 files changed

+17
-17
lines changed

14 files changed

+17
-17
lines changed

packages/coverage-v8/src/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class V8CoverageProvider extends BaseCoverageProvider<ResolvedCoverageOpt
166166
}
167167

168168
// Do not use pathToFileURL to avoid encoding filename parts
169-
const url = `file://${filename.startsWith('/') ? '' : '/'}${filename}`
169+
const url = `file://${filename[0] === '/' ? '' : '/'}${filename}`
170170

171171
const sources = await this.getSources(
172172
url,

packages/runner/src/fixture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function getUsedProps(fn: Function) {
366366
}
367367
}
368368

369-
if (!(first.startsWith('{') && first.endsWith('}'))) {
369+
if (!(first[0] === '{' && first.endsWith('}'))) {
370370
throw new Error(
371371
`The first argument inside a fixture must use object destructuring pattern, e.g. ({ test } => {}). Instead, received "${first}".`,
372372
)

packages/snapshot/src/port/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function addExtraLineBreaks(string: string): string {
7272
// Instead of trim, which can remove additional newlines or spaces
7373
// at beginning or end of the content from a custom serializer.
7474
export function removeExtraLineBreaks(string: string): string {
75-
return string.length > 2 && string.startsWith('\n') && string.endsWith('\n')
75+
return string.length > 2 && string[0] === '\n' && string.endsWith('\n')
7676
? string.slice(1, -1)
7777
: string
7878
}

packages/vite-node/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ function parseServerOptions(
192192
inline:
193193
inlineOptions !== true
194194
? inlineOptions.map((dep) => {
195-
return dep.startsWith('/') && dep.endsWith('/')
195+
return dep[0] === '/' && dep.endsWith('/')
196196
? new RegExp(dep)
197197
: dep
198198
})
199199
: true,
200200
external: toArray(serverOptions.deps?.external).map((dep) => {
201-
return dep.startsWith('/') && dep.endsWith('/') ? new RegExp(dep) : dep
201+
return dep[0] === '/' && dep.endsWith('/') ? new RegExp(dep) : dep
202202
}),
203203
moduleDirectories: serverOptions.deps?.moduleDirectories
204204
? toArray(serverOptions.deps?.moduleDirectories)

packages/vite-node/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class ViteNodeServer {
129129

130130
options.deps.moduleDirectories = options.deps.moduleDirectories.map(
131131
(dir) => {
132-
if (!dir.startsWith('/')) {
132+
if (dir[0] !== '/') {
133133
dir = `/${dir}`
134134
}
135135
if (!dir.endsWith('/')) {

packages/vite-node/src/source-map.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function withInlineSourcemap(
4141
if (isAbsolute(source)) {
4242
const actualPath
4343
= !source.startsWith(withTrailingSlash(options.root))
44-
&& source.startsWith('/')
44+
&& source[0] === '/'
4545
? resolve(options.root, source.slice(1))
4646
: source
4747
return relative(dirname(options.filepath), actualPath)
@@ -63,7 +63,7 @@ export function withInlineSourcemap(
6363
// so that debuggers can be set to break on first line
6464
// Since Vite 6, import statements at the top of the file are preserved correctly,
6565
// so we don't need to add this mapping anymore.
66-
if (!options.noFirstLineMapping && map.mappings.startsWith(';')) {
66+
if (!options.noFirstLineMapping && map.mappings[0] === ';') {
6767
map.mappings = `AAAA,CAAA${map.mappings}`
6868
}
6969

packages/vite-node/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function toFilePath(
137137
return { absolute: id.slice(4), exists: true }
138138
}
139139
// check if /src/module.js -> <root>/src/module.js
140-
if (!id.startsWith(withTrailingSlash(root)) && id.startsWith('/')) {
140+
if (!id.startsWith(withTrailingSlash(root)) && id[0] === '/') {
141141
const resolved = resolve(root, id.slice(1))
142142
if (existsSync(cleanUrl(resolved))) {
143143
return { absolute: resolved, exists: true }
@@ -159,7 +159,7 @@ export function toFilePath(
159159
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
160160
return {
161161
path:
162-
isWindows && absolute.startsWith('/')
162+
isWindows && absolute[0] === '/'
163163
? slash(fileURLToPath(pathToFileURL(absolute.slice(1)).href))
164164
: absolute,
165165
exists,

packages/vitest/src/node/cli/cac.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function removeQuotes<T>(str: T): T {
205205
}
206206
return str
207207
}
208-
if (str.startsWith('"') && str.endsWith('"')) {
208+
if (str[0] === '"' && str.endsWith('"')) {
209209
return str.slice(1, -1) as unknown as T
210210
}
211211
if (str.startsWith(`'`) && str.endsWith(`'`)) {

packages/vitest/src/node/config/resolveConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export function resolveConfig(
364364

365365
resolved.deps.moduleDirectories = resolved.deps.moduleDirectories.map(
366366
(dir) => {
367-
if (!dir.startsWith('/')) {
367+
if (dir[0] !== '/') {
368368
dir = `/${dir}`
369369
}
370370
if (!dir.endsWith('/')) {

packages/vitest/src/node/environments/fetchModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function inlineSourceMap(result: TransformResult) {
169169

170170
// If the first line is not present on source maps, add simple 1:1 mapping ([0,0,0,0], [1,0,0,0])
171171
// so that debuggers can be set to break on first line
172-
if (sourceMap.mappings.startsWith(';')) {
172+
if (sourceMap.mappings[0] === ';') {
173173
sourceMap.mappings = `AAAA,CAAA${sourceMap.mappings}`
174174
}
175175

0 commit comments

Comments
 (0)