From d1b450c42bebb1594428e73c21ab12d206b8d718 Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Fri, 26 Jan 2024 22:52:36 +0100 Subject: [PATCH 1/3] perf: check if Node Buffer is available once --- src/alloc.ts | 22 ++++++++++------------ src/util/as-uint8array.ts | 9 ++++----- 2 files changed, 14 insertions(+), 17 deletions(-) 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/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 +})() From b0d505127638a8bab62c32e0680d9967809d2bf0 Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Fri, 26 Jan 2024 22:53:20 +0100 Subject: [PATCH 2/3] perf: check if Node Buffer is available once and rm unnecessary asUint8Array call --- src/concat.ts | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) 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 + } +)() From 8832c888d950bad602751be8ad5a5a5a88e062b7 Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Fri, 26 Jan 2024 22:53:31 +0100 Subject: [PATCH 3/3] perf: rm unnecessary asUint8Array call --- src/xor.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 }