Skip to content

Commit 9f18801

Browse files
authored
perf: faster hashing for config object (#73)
1 parent 677c185 commit 9f18801

File tree

3 files changed

+12
-63
lines changed

3 files changed

+12
-63
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
}
8181
},
8282
"dependencies": {
83-
"oxc-resolver": "^5.0.0"
83+
"oxc-resolver": "^5.0.0",
84+
"stable-hash": "0.0.5"
8485
},
8586
"devDependencies": {
8687
"@types/node": "^22.13.10",

pnpm-lock.yaml

Lines changed: 8 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import type { NapiResolveOptions } from 'oxc-resolver'
2-
import { createHash } from 'node:crypto'
32
import { existsSync } from 'node:fs'
43
import { resolve } from 'node:path'
54
import { cwd } from 'node:process'
65
import { pathToFileURL } from 'node:url'
76
import { cloneDeep, isPlainObject, mergeWith } from 'es-toolkit'
87

9-
export const hashCache: WeakMap<NapiResolveOptions, string> = new WeakMap<NapiResolveOptions, string>()
10-
11-
export function generateHash(obj: any): string {
12-
function normalize(obj: any): string {
13-
if (obj === null || obj === 'undefined') {
14-
return 'null'
15-
} else if (typeof obj !== 'object') {
16-
return obj.toString()
17-
} else if (Array.isArray(obj)) {
18-
return `[${obj.map(normalize).sort().join(',')}]`
19-
}
20-
const sortedKeys = Object.keys(obj).sort()
21-
return `{${sortedKeys.map((key) => `${key}:${normalize(obj[key])}`).join(',')}}`
22-
}
8+
import { stableHash as generateHash } from 'stable-hash'
239

24-
const normalizedString = normalize(obj)
25-
return createHash('md5').update(normalizedString).digest('hex')
26-
}
10+
export const hashCache: WeakMap<NapiResolveOptions, string> = new WeakMap<NapiResolveOptions, string>()
2711

2812
export function hashObject(obj: NapiResolveOptions): string {
2913
if (hashCache.has(obj)) {

0 commit comments

Comments
 (0)