diff --git a/src/alloc.ts b/src/alloc.ts index ed3d5f9..3e28ebc 100644 --- a/src/alloc.ts +++ b/src/alloc.ts @@ -4,23 +4,21 @@ import { asUint8Array } from './util/as-uint8array.js' * Returns a `Uint8Array` of the requested size. Referenced memory will * be initialized to 0. */ -export function alloc (size: number = 0): Uint8Array { - if (globalThis.Buffer?.alloc != null) { - return asUint8Array(globalThis.Buffer.alloc(size)) +export const alloc = (() => { + if (globalThis.Buffer.alloc != null) { + return (len: number = 0) => asUint8Array(globalThis.Buffer.alloc(len)) } - - return new Uint8Array(size) -} + return (len: number = 0) => new Uint8Array(len) +})() /** * Where possible returns a Uint8Array of the requested size that references * uninitialized memory. Only use if you are certain you will immediately * overwrite every value in the returned `Uint8Array`. */ -export function allocUnsafe (size: number = 0): Uint8Array { - if (globalThis.Buffer?.allocUnsafe != null) { - return asUint8Array(globalThis.Buffer.allocUnsafe(size)) +export const allocUnsafe = (() => { + if (globalThis.Buffer.allocUnsafe != null) { + return (len: number = 0) => asUint8Array(globalThis.Buffer.allocUnsafe(len)) } - - return new Uint8Array(size) -} + return (len: number = 0) => new Uint8Array(len) +})() diff --git a/src/concat.ts b/src/concat.ts index 71ad369..f03f2d0 100644 --- a/src/concat.ts +++ b/src/concat.ts @@ -4,22 +4,24 @@ import { asUint8Array } from './util/as-uint8array.js' /** * Returns a new Uint8Array created by concatenating the passed Uint8Arrays */ -export function concat (arrays: Uint8Array[], length?: number): Uint8Array { - if (globalThis.Buffer != null) { - return asUint8Array(globalThis.Buffer.concat(arrays, length)) - } +export const concat = (():((arrays: Uint8Array[], length?: number) => Uint8Array) => globalThis.Buffer != null + ? (arrays, length?) => asUint8Array(globalThis.Buffer.concat(arrays, length)) + : (arrays, length?) => { + if (length == null) { + length = 0 + for (const array of arrays) { + length += array.length + } + } - if (length == null) { - length = arrays.reduce((acc, curr) => acc + curr.length, 0) - } + const output = allocUnsafe(length) + let offset = 0 - const output = allocUnsafe(length) - let offset = 0 + for (const arr of arrays) { + output.set(arr, offset) + offset += arr.length + } - for (const arr of arrays) { - output.set(arr, offset) - offset += arr.length - } - - return asUint8Array(output) -} + return output + } +)() diff --git a/src/util/as-uint8array.ts b/src/util/as-uint8array.ts index 9a24af5..a544f85 100644 --- a/src/util/as-uint8array.ts +++ b/src/util/as-uint8array.ts @@ -2,10 +2,9 @@ * To guarantee Uint8Array semantics, convert nodejs Buffers * into vanilla Uint8Arrays */ -export function asUint8Array (buf: Uint8Array): Uint8Array { +export const asUint8Array = (() => { if (globalThis.Buffer != null) { - return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) + return (buf: Uint8Array | globalThis.Buffer): Uint8Array => buf.constructor !== Uint8Array ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf } - - return buf -} + return (buf: Uint8Array) => buf +})() diff --git a/src/xor.ts b/src/xor.ts index e53435d..9d77bba 100644 --- a/src/xor.ts +++ b/src/xor.ts @@ -1,5 +1,4 @@ import { allocUnsafe } from './alloc.js' -import { asUint8Array } from './util/as-uint8array.js' /** * Returns the xor distance between two arrays @@ -15,5 +14,5 @@ export function xor (a: Uint8Array, b: Uint8Array): Uint8Array { result[i] = a[i] ^ b[i] } - return asUint8Array(result) + return result }