refactor: remove unnecessary array allocation

If the `packetsFn` array is empty, there is no need to allocate one new
array.
This commit is contained in:
Damien Arrachequesne
2024-06-14 01:07:48 +02:00
parent 407c3ad236
commit 62f59b6cf3

View File

@@ -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);