Skip to content

Commit 8d6c24b

Browse files
authored
fix: use Buffer.concat on node as it is faster (#73)
Running the concat.js benchmark: Before: ``` Uint8Arrays.concat x 792,619 ops/sec ±0.67% (98 runs sampled) Uint8Arrays.concat with length x 782,264 ops/sec ±0.18% (98 runs sampled) Uint8Array.set x 799,528 ops/sec ±0.67% (92 runs sampled) allocUnsafe.set x 851,403 ops/sec ±0.24% (97 runs sampled) Fastest is allocUnsafe.set ``` After: ``` Uint8Arrays.concat x 896,831 ops/sec ±0.20% (101 runs sampled) Uint8Arrays.concat with length x 887,523 ops/sec ±0.19% (99 runs sampled) Uint8Array.set x 814,749 ops/sec ±0.46% (98 runs sampled) allocUnsafe.set x 885,140 ops/sec ±0.28% (98 runs sampled) Fastest is Uint8Arrays.concat ```
1 parent 3706db6 commit 8d6c24b

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

src/compare.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* Can be used with Array.sort to sort and array with Uint8Array entries
33
*/
44
export function compare (a: Uint8Array, b: Uint8Array): number {
5+
if (globalThis.Buffer != null) {
6+
return globalThis.Buffer.compare(a, b)
7+
}
8+
59
for (let i = 0; i < a.byteLength; i++) {
610
if (a[i] < b[i]) {
711
return -1

src/concat.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { asUint8Array } from './util/as-uint8array.js'
44
/**
55
* Returns a new Uint8Array created by concatenating the passed ArrayLikes
66
*/
7-
export function concat (arrays: Array<ArrayLike<number>>, length?: number): Uint8Array {
7+
export function concat (arrays: Uint8Array[], length?: number): Uint8Array {
8+
if (globalThis.Buffer != null) {
9+
return asUint8Array(globalThis.Buffer.concat(arrays, length))
10+
}
11+
812
if (length == null) {
913
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
1014
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
*
5959
* ## concat(arrays, \[length])
6060
*
61-
* Concatenate one or more array-likes and return a `Uint8Array` with their contents.
61+
* Concatenate one or more `Uint8Array`s and return a `Uint8Array` with their contents.
6262
*
6363
* 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.
6464
*

test/concat.spec.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env mocha */
22

33
import { expect } from 'aegir/chai'
4+
import { alloc } from '../src/alloc.js'
45
import { concat } from '../src/concat.js'
56

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

23-
it('concats mixed Uint8Arrays and Arrays', () => {
24-
const a = Uint8Array.from([0, 1, 2, 3])
25-
const b = [4, 5, 6, 7]
26-
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7])
27-
28-
expect(concat([a, b])).to.deep.equal(c)
29-
})
30-
31-
it('concats mixed Uint8Arrays and Arrays with a length', () => {
32-
const a = Uint8Array.from([0, 1, 2, 3])
33-
const b = [4, 5, 6, 7]
34-
const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7])
35-
36-
expect(concat([a, b], 8)).to.deep.equal(c)
37-
})
38-
3924
it('concat returns Uint8Array', () => {
4025
const a = Uint8Array.from([0, 1, 2, 3])
41-
const b = [4, 5, 6, 7]
26+
const b = alloc(10).fill(1)
4227
const c = concat([a, b])
4328
const slice = c.slice()
4429

0 commit comments

Comments
 (0)