diff --git a/lib/net.js b/lib/net.js index b47c540ac8b692..d0d7cfcb92addc 100644 --- a/lib/net.js +++ b/lib/net.js @@ -807,19 +807,18 @@ protoGetter('_bytesDispatched', function _bytesDispatched() { protoGetter('bytesWritten', function bytesWritten() { let bytes = this._bytesDispatched; - const state = this._writableState; const data = this._pendingData; const encoding = this._pendingEncoding; + const writableBuffer = this.writableBuffer; - if (!state) + if (!writableBuffer) return undefined; - this.writableBuffer.forEach(function(el) { - if (el.chunk instanceof Buffer) - bytes += el.chunk.length; - else - bytes += Buffer.byteLength(el.chunk, el.encoding); - }); + for (const el of writableBuffer) { + bytes += el.chunk instanceof Buffer ? + el.chunk.length : + Buffer.byteLength(el.chunk, el.encoding); + } if (ArrayIsArray(data)) { // Was a writev, iterate over chunks to get total length