From 62f59b6cf3b2f073820d0a8cff70682bc1d4ba9e Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 14 Jun 2024 01:07:48 +0200 Subject: [PATCH] refactor: remove unnecessary array allocation If the `packetsFn` array is empty, there is no need to allocate one new array. --- lib/socket.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/socket.ts b/lib/socket.ts index bac970c2..300fcb19 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -251,8 +251,10 @@ export class Socket extends EventEmitter { if (this.sentCallbackFn.length > 0) { debug("executing batch send callback"); const seqFn = this.sentCallbackFn.shift(); - for (let i = 0; i < seqFn.length; i++) { - seqFn[i](this.transport); + if (seqFn) { + for (let i = 0; i < seqFn.length; i++) { + seqFn[i](this.transport); + } } } } @@ -491,8 +493,14 @@ export class Socket extends EventEmitter { this.server.emit("flush", this, this.writeBuffer); const wbuf = this.writeBuffer; this.writeBuffer = []; - this.sentCallbackFn.push(this.packetsFn); - this.packetsFn = []; + + if (this.packetsFn.length) { + this.sentCallbackFn.push(this.packetsFn); + this.packetsFn = []; + } else { + this.sentCallbackFn.push(null); + } + this.transport.send(wbuf); this.emit("drain"); this.server.emit("drain", this);