stream: improve read() performance further
PR-URL: https://github.com/nodejs/node/pull/29077 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
f114e5ba33
commit
382f84a7f8
@ -71,19 +71,19 @@ module.exports = class BufferList {
|
|||||||
|
|
||||||
// Consumes a specified amount of bytes or characters from the buffered data.
|
// Consumes a specified amount of bytes or characters from the buffered data.
|
||||||
consume(n, hasStrings) {
|
consume(n, hasStrings) {
|
||||||
var ret;
|
const data = this.head.data;
|
||||||
if (n < this.head.data.length) {
|
if (n < data.length) {
|
||||||
// `slice` is the same for buffers and strings.
|
// `slice` is the same for buffers and strings.
|
||||||
ret = this.head.data.slice(0, n);
|
const slice = data.slice(0, n);
|
||||||
this.head.data = this.head.data.slice(n);
|
this.head.data = data.slice(n);
|
||||||
} else if (n === this.head.data.length) {
|
return slice;
|
||||||
// First chunk is a perfect match.
|
|
||||||
ret = this.shift();
|
|
||||||
} else {
|
|
||||||
// Result spans more than one buffer.
|
|
||||||
ret = hasStrings ? this._getString(n) : this._getBuffer(n);
|
|
||||||
}
|
}
|
||||||
return ret;
|
if (n === data.length) {
|
||||||
|
// First chunk is a perfect match.
|
||||||
|
return this.shift();
|
||||||
|
}
|
||||||
|
// Result spans more than one buffer.
|
||||||
|
return hasStrings ? this._getString(n) : this._getBuffer(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
first() {
|
first() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user