-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: always return uint8arrays (#39)
3.1.0 incorporated internal refactors to use node `Buffer`s where possible to increase performance when `alloc`ing new byte arrays. It then returns those `Buffer`s but the problem is `Buffer` is not completely compatible with `Uint8Array` as some methods with the same name behave differently. We can convert a `Buffer` to a `Uint8Array` without copying it by using the 3-arg `Uint8Array` constructor so we should do that to retain the performance characteristics of `Buffer` when `alloc`ing but the compatibility of returning vanilla `Uint8Array`s at the cost of a performance hit to `Uint8Arrays.allocUnsafe` (see the added `alloc.js` benchmark). Before: ``` Uint8Arrays.alloc x 1,559,446 ops/sec ±2.00% (79 runs sampled) Uint8Arrays.allocUnsafe x 5,410,575 ops/sec ±1.11% (90 runs sampled) new Uint8Array x 1,757,101 ops/sec ±1.85% (79 runs sampled) Buffer.alloc x 1,691,343 ops/sec ±2.17% (79 runs sampled) Buffer.allocUnsafe x 6,928,848 ops/sec ±1.18% (89 runs sampled) Fastest is Buffer.allocUnsafe ``` After: ``` Uint8Arrays.alloc x 1,480,130 ops/sec ±2.64% (80 runs sampled) Uint8Arrays.allocUnsafe x 4,425,871 ops/sec ±0.91% (91 runs sampled) new Uint8Array x 1,723,491 ops/sec ±2.62% (75 runs sampled) Buffer.alloc x 1,697,649 ops/sec ±2.49% (79 runs sampled) Buffer.allocUnsafe x 6,662,341 ops/sec ±1.25% (88 runs sampled) Fastest is Buffer.allocUnsafe ``` Fixes #38
- Loading branch information
1 parent
56329d1
commit 017a456
Showing
11 changed files
with
165 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/* | ||
$ node benchmarks/alloc.js | ||
$ npx playwright-test benchmarks/alloc.js --runner benchmark | ||
*/ | ||
|
||
import Benchmark from 'benchmark' | ||
import { alloc, allocUnsafe } from '../src/alloc.js' | ||
|
||
const LENGTH = 1024 | ||
|
||
function checkAlloc (arr) { | ||
return arr.byteLength !== LENGTH | ||
} | ||
|
||
const suite = new Benchmark.Suite() | ||
|
||
suite | ||
.add('Uint8Arrays.alloc', () => { | ||
const res = alloc(LENGTH) | ||
|
||
if (checkAlloc(res)) { | ||
throw new Error('Alloc failed') | ||
} | ||
}) | ||
.add('Uint8Arrays.allocUnsafe', () => { | ||
const res = allocUnsafe(LENGTH) | ||
|
||
if (checkAlloc(res)) { | ||
throw new Error('Alloc failed') | ||
} | ||
}) | ||
.add('new Uint8Array', () => { | ||
const res = new Uint8Array(LENGTH) | ||
|
||
if (checkAlloc(res)) { | ||
throw new Error('Alloc failed') | ||
} | ||
}) | ||
|
||
if (globalThis.Buffer != null) { | ||
suite.add('Buffer.alloc', function () { | ||
const res = globalThis.Buffer.alloc(LENGTH) | ||
|
||
if (checkAlloc(res)) { | ||
throw new Error('Alloc failed') | ||
} | ||
}) | ||
suite.add('Buffer.allocUnsafe', function () { | ||
const res = globalThis.Buffer.allocUnsafe(LENGTH) | ||
|
||
if (checkAlloc(res)) { | ||
throw new Error('Alloc failed') | ||
} | ||
}) | ||
} | ||
|
||
suite | ||
// add listeners | ||
.on('cycle', (event) => { | ||
console.log(String(event.target)) | ||
}) | ||
.on('complete', function () { | ||
console.log('Fastest is ' + this.filter('fastest').map('name')) | ||
}) | ||
// run async | ||
.run({ async: true }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
/** | ||
* To guarantee Uint8Array semantics, convert nodejs Buffers | ||
* into vanilla Uint8Arrays | ||
* | ||
* @param {Uint8Array} buf | ||
* @returns {Uint8Array} | ||
*/ | ||
export function asUint8Array (buf) { | ||
if (globalThis.Buffer != null) { | ||
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) | ||
} | ||
|
||
return buf | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters