Skip to content

Avoid using Buffer.concat() #1026

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

Merged
merged 1 commit into from
Mar 2, 2017
Merged

Avoid using Buffer.concat() #1026

merged 1 commit into from
Mar 2, 2017

Conversation

lpinca
Copy link
Member

@lpinca lpinca commented Mar 1, 2017

This patch adds a new function to merge an array of buffers and replaces all occurrences of Buffer.concat() with it.

The reason for this change is that Buffer.concat() checks that every element in the array is an instance of Buffer and this makes it perform worse.

@lpinca lpinca force-pushed the avoid/buffer-concat branch from ed8cb01 to dd2f745 Compare March 1, 2017 18:13
@lpinca lpinca force-pushed the avoid/buffer-concat branch from dd2f745 to 00bf145 Compare March 1, 2017 18:24
Copy link
Member

@3rd-Eden 3rd-Eden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lpinca
Copy link
Member Author

lpinca commented Mar 2, 2017

We should find a way to remove all instanceof checks in the code base. It's unbelievably slow.

@lpinca lpinca merged commit 4ae630a into master Mar 2, 2017
@lpinca lpinca deleted the avoid/buffer-concat branch March 2, 2017 14:43
@3rd-Eden
Copy link
Member

3rd-Eden commented Mar 2, 2017

Is it slower than Object.prototype.toString.call(item)?

@3rd-Eden
Copy link
Member

3rd-Eden commented Mar 2, 2017

And as far as I could see there's only 1, which is in sender.

@lpinca
Copy link
Member Author

lpinca commented Mar 2, 2017

Buffer.isBuffer() also uses instanceof internally but yes I think we only have those checks in the sender.

@lpinca
Copy link
Member Author

lpinca commented Mar 2, 2017

The fastest way is probably to use thing.constructor === Buffer but it will not work if thing is a subclass.

@lpinca
Copy link
Member Author

lpinca commented Mar 2, 2017

Object.prototype.toString() x 1,927,356 ops/sec ±0.99% (82 runs sampled)
Buffer.isBuffer() x 3,755,305 ops/sec ±0.88% (85 runs sampled)
process.binding('util').isUint8Array() x 23,694,865 ops/sec ±1.97% (82 runs sampled)
.constructor x 52,849,107 ops/sec ±2.57% (79 runs sampled)

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.

2 participants