Skip to content

Commit 5d07bba

Browse files
authored
perf: use hash to replace createHash (#6703)
1 parent a61293e commit 5d07bba

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

packages/vitest/src/integrations/css/css-modules.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { createHash } from 'node:crypto'
1+
import { hash } from '../../node/hash'
22
import type { CSSModuleScopeStrategy } from '../../node/types/config'
33

44
export function generateCssFilenameHash(filepath: string) {
5-
return createHash('md5').update(filepath).digest('hex').slice(0, 6)
5+
return hash('md5', filepath, 'hex').slice(0, 6)
66
}
77

88
export function generateScopedClassName(

packages/vitest/src/node/cache/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import crypto from 'node:crypto'
21
import { resolve } from 'pathe'
32
import { slash } from '../../utils'
3+
import { hash } from '../hash'
44
import { FilesStatsCache } from './files'
55
import { ResultsCache } from './results'
66

@@ -26,7 +26,7 @@ export class VitestCache {
2626
? resolve(
2727
root,
2828
baseDir,
29-
crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'),
29+
hash('md5', projectName, 'hex'),
3030
)
3131
: resolve(root, baseDir)
3232
}

packages/vitest/src/node/hash.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import crypto from 'node:crypto'
2+
3+
export const hash = crypto.hash ?? ((
4+
algorithm: string,
5+
data: crypto.BinaryLike,
6+
outputEncoding: crypto.BinaryToTextEncoding,
7+
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

packages/vitest/src/node/pools/rpc.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { createHash } from 'node:crypto'
21
import { mkdir, writeFile } from 'node:fs/promises'
32
import type { RawSourceMap } from 'vite-node'
43
import { join } from 'pathe'
54
import type { WorkspaceProject } from '../workspace'
65
import type { RuntimeRPC } from '../../types/rpc'
6+
import { hash } from '../hash'
77

88
const created = new Set()
99
const promises = new Map<string, Promise<void>>()
@@ -47,7 +47,7 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
4747
}
4848

4949
const dir = join(project.tmpDir, transformMode)
50-
const name = createHash('sha1').update(id).digest('hex')
50+
const name = hash('sha1', id, 'hex')
5151
const tmp = join(dir, name)
5252
if (promises.has(tmp)) {
5353
await promises.get(tmp)

packages/vitest/src/node/sequencers/BaseSequencer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createHash } from 'node:crypto'
21
import { relative, resolve } from 'pathe'
32
import { slash } from 'vite-node/utils'
3+
import { hash } from '../hash'
44
import type { Vitest } from '../core'
55
import type { WorkspaceSpec } from '../pool'
66
import type { TestSequencer } from './types'
@@ -25,7 +25,7 @@ export class BaseSequencer implements TestSequencer {
2525
const specPath = fullPath?.slice(config.root.length)
2626
return {
2727
spec,
28-
hash: createHash('sha1').update(specPath).digest('hex'),
28+
hash: hash('sha1', specPath, 'hex'),
2929
}
3030
})
3131
.sort((a, b) => (a.hash < b.hash ? -1 : a.hash > b.hash ? 1 : 0))

0 commit comments

Comments
 (0)