Skip to content

Commit 05af499

Browse files
committed
2 parents 3e17072 + 775dd7c commit 05af499

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

src/utils/crypto.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type Algorithm = {
1212

1313
type Data = string | boolean | number | JSONValue | ArrayBufferView | ArrayBuffer
1414

15+
// Reuse TextEncoder instance to avoid repeated allocations
16+
const encoder = new TextEncoder()
17+
1518
export const sha256 = async (data: Data): Promise<string | null> => {
1619
const algorithm: Algorithm = { name: 'SHA-256', alias: 'sha256' }
1720
const hash = await createHash(data, algorithm)
@@ -39,7 +42,7 @@ export const createHash = async (data: Data, algorithm: Algorithm): Promise<stri
3942
if (typeof data === 'object') {
4043
data = JSON.stringify(data)
4144
}
42-
sourceBuffer = new TextEncoder().encode(String(data))
45+
sourceBuffer = encoder.encode(String(data))
4346
}
4447

4548
if (crypto && crypto.subtle) {

0 commit comments

Comments
 (0)