-
Notifications
You must be signed in to change notification settings - Fork 26
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
perf: use Buffer.allocUnsafe where available #29
Conversation
If `Buffer.allocUnsafe` is available, and we know we are about to fill the returned vales, use it in preference to `new Uint8Array` as it is much faster.
Codecov Report
@@ Coverage Diff @@
## master #29 +/- ##
==========================================
- Coverage 98.15% 97.45% -0.70%
==========================================
Files 7 8 +1
Lines 217 236 +19
Branches 44 47 +3
==========================================
+ Hits 213 230 +17
- Misses 4 6 +2
Continue to review full report at Codecov.
|
* @param {number} length | ||
* @returns {Uint8Array} | ||
*/ | ||
export function allocUnsafe (length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this could be exported too? Used in protons?
* @returns {Uint8Array} | ||
*/ | ||
export function allocUnsafe (length) { | ||
if (globalThis.Buffer != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the protons version looks like if (globalThis?.Buffer?.allocUnsafe != null) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's probably overkill - globalThis
exists everywhere supported so just globalThis.Buffer?.allocUnsafe
would do.
Superseded by #34 |
If
Buffer.allocUnsafe
is available, and we know we are about to fill the returned vales, use it in preference tonew Uint8Array
as it is much faster.