Skip to content

Commit

Permalink
refactor: remove unnecessary array allocation
Browse files Browse the repository at this point in the history
If the `packetsFn` array is empty, there is no need to allocate one new
array.
  • Loading branch information
darrachequesne committed Jun 13, 2024
1 parent 407c3ad commit 62f59b6
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 62f59b6

Please sign in to comment.