Skip to content

perf: Check only if Buffer is available once and remove redundant asUint8array calls #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 10 additions & 12 deletions src/alloc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})()
34 changes: 18 additions & 16 deletions src/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)()
9 changes: 4 additions & 5 deletions src/util/as-uint8array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})()
3 changes: 1 addition & 2 deletions src/xor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { allocUnsafe } from './alloc.js'
import { asUint8Array } from './util/as-uint8array.js'

/**
* Returns the xor distance between two arrays
Expand All @@ -15,5 +14,5 @@ export function xor (a: Uint8Array, b: Uint8Array): Uint8Array {
result[i] = a[i] ^ b[i]
}

return asUint8Array(result)
return result
}