Skip to content
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

fix: use Buffer.concat on node as it is faster #73

Merged
merged 1 commit into from
Nov 24, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* Can be used with Array.sort to sort and array with Uint8Array entries
*/
export function compare (a: Uint8Array, b: Uint8Array): number {
if (globalThis.Buffer != null) {
return globalThis.Buffer.compare(a, b)
}

for (let i = 0; i < a.byteLength; i++) {
if (a[i] < b[i]) {
return -1
Expand Down
6 changes: 5 additions & 1 deletion src/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { asUint8Array } from './util/as-uint8array.js'
/**
* Returns a new Uint8Array created by concatenating the passed ArrayLikes
*/
export function concat (arrays: Array<ArrayLike<number>>, length?: number): Uint8Array {
export function concat (arrays: Uint8Array[], length?: number): Uint8Array {
if (globalThis.Buffer != null) {
return asUint8Array(globalThis.Buffer.concat(arrays, length))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately arrays here causes Buffer.concat to throw, where before arrays would have been converted to a Uint8Array on line 24.

}

if (length == null) {
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
*
* ## concat(arrays, \[length])
*
* Concatenate one or more array-likes and return a `Uint8Array` with their contents.
* Concatenate one or more `Uint8Array`s and return a `Uint8Array` with their contents.
*
* If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.
*
Expand Down
19 changes: 2 additions & 17 deletions test/concat.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { alloc } from '../src/alloc.js'
import { concat } from '../src/concat.js'

describe('Uint8Array concat', () => {
Expand All @@ -20,25 +21,9 @@ describe('Uint8Array concat', () => {
expect(concat([a, b], 8)).to.deep.equal(c)
})

it('concats mixed Uint8Arrays and Arrays', () => {
const a = Uint8Array.from([0, 1, 2, 3])
const b = [4, 5, 6, 7]
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7])

expect(concat([a, b])).to.deep.equal(c)
})

it('concats mixed Uint8Arrays and Arrays with a length', () => {
const a = Uint8Array.from([0, 1, 2, 3])
const b = [4, 5, 6, 7]
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7])

expect(concat([a, b], 8)).to.deep.equal(c)
})

it('concat returns Uint8Array', () => {
const a = Uint8Array.from([0, 1, 2, 3])
const b = [4, 5, 6, 7]
const b = alloc(10).fill(1)
const c = concat([a, b])
const slice = c.slice()

Expand Down