Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/byte-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ export function asU8A (buf) {
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf
}

// Threshold for manual UTF-8 encoding vs native methods.
// Node.js Buffer.from: crossover ~24 chars
// Browser TextEncoder: crossover ~200 chars
const FROM_STRING_THRESHOLD_BUFFER = 24
const FROM_STRING_THRESHOLD_TEXTENCODER = 200

export const fromString = useBuffer
? // eslint-disable-line operator-linebreak
/**
* @param {string} string
*/
(string) => {
return string.length > 64
return string.length >= FROM_STRING_THRESHOLD_BUFFER
? // eslint-disable-line operator-linebreak
// @ts-ignore
globalThis.Buffer.from(string)
Expand All @@ -51,7 +57,7 @@ export const fromString = useBuffer
* @param {string} string
*/
(string) => {
return string.length > 64 ? textEncoder.encode(string) : utf8ToBytes(string)
return string.length >= FROM_STRING_THRESHOLD_TEXTENCODER ? textEncoder.encode(string) : utf8ToBytes(string)
}

/**
Expand Down