-
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.
feat: add alloc and allocUnsafe (#31)
Allows us to create `Uint8Array`s that reference unitialised memory where supported in order to improve performance.
- Loading branch information
1 parent
02a8abb
commit bd7147f
Showing
6 changed files
with
96 additions
and
11 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
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,30 @@ | ||
/** | ||
* Returns a `Uint8Array` of the requested size. Referenced memory will | ||
* be initialized to 0. | ||
* | ||
* @param {number} [size] | ||
* @returns {Uint8Array} | ||
*/ | ||
export function alloc (size = 0) { | ||
if (globalThis.Buffer != null && globalThis.Buffer.alloc != null) { | ||
return globalThis.Buffer.alloc(size) | ||
} | ||
|
||
return new Uint8Array(size) | ||
} | ||
|
||
/** | ||
* 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`. | ||
* | ||
* @param {number} [size] | ||
* @returns {Uint8Array} | ||
*/ | ||
export function allocUnsafe (size = 0) { | ||
if (globalThis.Buffer != null && globalThis.Buffer.allocUnsafe != null) { | ||
return globalThis.Buffer.allocUnsafe(size) | ||
} | ||
|
||
return new Uint8Array(size) | ||
} |
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,25 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/utils/chai.js' | ||
import { alloc, allocUnsafe } from '../src/alloc.js' | ||
|
||
describe('Uint8Array alloc', () => { | ||
it('can alloc memory', () => { | ||
const size = 10 | ||
|
||
expect(alloc(size)).to.have.property('byteLength', size) | ||
}) | ||
|
||
it('can alloc memory', () => { | ||
const size = 10 | ||
const buf = alloc(size) | ||
|
||
expect(buf.every(value => value === 0)).to.be.true() | ||
}) | ||
|
||
it('can alloc memory unsafely', () => { | ||
const size = 10 | ||
|
||
expect(allocUnsafe(size)).to.have.property('byteLength', size) | ||
}) | ||
}) |
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