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

perf: use Buffer.allocUnsafe where available #29

Closed
wants to merge 3 commits into from

Conversation

achingbrain
Copy link
Owner

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.

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-commenter
Copy link

codecov-commenter commented Jul 25, 2022

Codecov Report

Merging #29 (285e8af) into master (02a8abb) will decrease coverage by 0.69%.
The diff coverage is 90.47%.

@@            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     
Impacted Files Coverage Δ
src/util/alloc-unsafe.js 87.50% <87.50%> (ø)
src/concat.js 100.00% <100.00%> (ø)
src/util/bases.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02a8abb...285e8af. Read the comment docs.

@achingbrain achingbrain marked this pull request as draft July 25, 2022 17:45
* @param {number} length
* @returns {Uint8Array}
*/
export function allocUnsafe (length) {
Copy link
Contributor

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) {
Copy link
Contributor

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) {

Copy link
Owner Author

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.

@achingbrain
Copy link
Owner Author

Superseded by #34

@achingbrain achingbrain closed this Aug 2, 2022
@achingbrain achingbrain deleted the perf/use-alloc-unsafe-where-available branch August 2, 2022 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants