From 2a844599db8b7bdbe200358a6acb23ed30a38954 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 10 Aug 2019 04:26:32 -0400 Subject: [PATCH] stream: improve read() performance further PR-URL: https://github.com/nodejs/node/pull/29077 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Yongsheng Zhang Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca --- lib/internal/streams/buffer_list.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/internal/streams/buffer_list.js b/lib/internal/streams/buffer_list.js index 36777ed82d4bcb..9d6e9e2fe4f7f9 100644 --- a/lib/internal/streams/buffer_list.js +++ b/lib/internal/streams/buffer_list.js @@ -71,19 +71,19 @@ module.exports = class BufferList { // Consumes a specified amount of bytes or characters from the buffered data. consume(n, hasStrings) { - var ret; - if (n < this.head.data.length) { + const data = this.head.data; + if (n < data.length) { // `slice` is the same for buffers and strings. - ret = this.head.data.slice(0, n); - this.head.data = this.head.data.slice(n); - } else if (n === this.head.data.length) { + const slice = data.slice(0, n); + this.head.data = data.slice(n); + return slice; + } + if (n === data.length) { // 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 this.shift(); } - return ret; + // Result spans more than one buffer. + return hasStrings ? this._getString(n) : this._getBuffer(n); } first() {