diff --git a/lib/buffer.js b/lib/buffer.js index d7d10007070e2c..cbe80605a9b074 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -352,13 +352,13 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) { const viewLength = TypedArrayPrototypeGetLength(view); if (viewLength === 0) { - return Buffer.alloc(0); + return new FastBuffer(); } if (offset !== undefined || length !== undefined) { if (offset !== undefined) { validateInteger(offset, 'offset', 0); - if (offset >= viewLength) return Buffer.alloc(0); + if (offset >= viewLength) return new FastBuffer(); } else { offset = 0; } @@ -1261,7 +1261,7 @@ if (internalBinding('config').hasIntl) { throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source); } - if (source.length === 0) return Buffer.alloc(0); + if (source.length === 0) return new FastBuffer(); fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding; toEncoding = normalizeEncoding(toEncoding) || toEncoding; diff --git a/lib/dgram.js b/lib/dgram.js index e1241ca793e912..78ad9cf93408f4 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -73,6 +73,7 @@ const { defaultTriggerAsyncIdScope, symbols: { async_id_symbol, owner_symbol }, } = require('internal/async_hooks'); +const { FastBuffer } = require('internal/buffer'); const { UV_UDP_REUSEADDR } = internalBinding('constants').os; const { @@ -688,7 +689,7 @@ Socket.prototype.send = function(buffer, this.bind({ port: 0, exclusive: true }, null); if (list.length === 0) - ArrayPrototypePush(list, Buffer.alloc(0)); + ArrayPrototypePush(list, new FastBuffer()); // If the socket hasn't been bound yet, push the outbound packet onto the // send queue and send after binding is complete. diff --git a/lib/internal/debugger/inspect_client.js b/lib/internal/debugger/inspect_client.js index 315617bf08a800..dc1fc8f011cf6d 100644 --- a/lib/internal/debugger/inspect_client.js +++ b/lib/internal/debugger/inspect_client.js @@ -16,6 +16,7 @@ const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes; const { EventEmitter } = require('events'); const http = require('http'); const { URL } = require('internal/url'); +const { FastBuffer } = require('internal/buffer'); const debuglog = require('internal/util/debuglog').debuglog('inspect'); @@ -79,7 +80,7 @@ function encodeFrameHybi17(payload) { additionalLength[0] = (dataLength & 0xFF00) >> 8; additionalLength[1] = dataLength & 0xFF; } else { - additionalLength = Buffer.alloc(0); + additionalLength = new FastBuffer(); singleByteLength = dataLength; } @@ -233,7 +234,7 @@ class Client extends EventEmitter { this._lastId = 0; this._socket = null; this._pending = {}; - this._unprocessed = Buffer.alloc(0); + this._unprocessed = new FastBuffer(); } callMethod(method, params) { diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index aab3be8a9e3560..126689854d326d 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -1605,7 +1605,7 @@ function fromList(n, state) { buf[idx++] = null; } } else if (len - idx === 0) { - ret = Buffer.alloc(0); + ret = new FastBuffer(); } else if (len - idx === 1) { ret = buf[idx]; buf[idx++] = null; diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 1340b817a57e5b..2b622e6be39eb0 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -78,6 +78,7 @@ const { kTestTimeoutFailure, Test, } = require('internal/test_runner/test'); +const { FastBuffer } = require('internal/buffer'); const { convertStringToRegExp, @@ -324,7 +325,7 @@ class FileTest extends Test { // This method is called when it is known that there is at least one message let bufferHead = this.#rawBuffer[0]; let headerIndex = bufferHead.indexOf(v8Header); - let nonSerialized = Buffer.alloc(0); + let nonSerialized = new FastBuffer(); while (bufferHead && headerIndex !== 0) { const nonSerializedData = headerIndex === -1 ? diff --git a/lib/zlib.js b/lib/zlib.js index 6fce059732f2da..1949b9c39f5eb3 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -70,6 +70,7 @@ const { validateUint32, validateFiniteNumber, } = require('internal/validators'); +const { FastBuffer } = require('internal/buffer'); const kFlushFlag = Symbol('kFlushFlag'); const kError = Symbol('kError'); @@ -150,7 +151,7 @@ function zlibBufferOnError(err) { function zlibBufferOnEnd() { let buf; if (this.nread === 0) { - buf = Buffer.alloc(0); + buf = new FastBuffer(); } else { const bufs = this.buffers; buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread)); @@ -301,7 +302,7 @@ ZlibBase.prototype.reset = function() { * @returns {void} */ ZlibBase.prototype._flush = function(callback) { - this._transform(Buffer.alloc(0), '', callback); + this._transform(new FastBuffer(), '', callback); }; /** @@ -476,7 +477,7 @@ function processChunkSync(self, chunk, flushFlag) { _close(self); if (nread === 0) - return Buffer.alloc(0); + return new FastBuffer(); return (buffers.length === 1 ? buffers[0] : Buffer.concat(buffers, nread)); }