Skip to content

Commit 62f59b6

Browse files
refactor: remove unnecessary array allocation
If the `packetsFn` array is empty, there is no need to allocate one new array.
1 parent 407c3ad commit 62f59b6

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/socket.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,10 @@ export class Socket extends EventEmitter {
251251
if (this.sentCallbackFn.length > 0) {
252252
debug("executing batch send callback");
253253
const seqFn = this.sentCallbackFn.shift();
254-
for (let i = 0; i < seqFn.length; i++) {
255-
seqFn[i](this.transport);
254+
if (seqFn) {
255+
for (let i = 0; i < seqFn.length; i++) {
256+
seqFn[i](this.transport);
257+
}
256258
}
257259
}
258260
}
@@ -491,8 +493,14 @@ export class Socket extends EventEmitter {
491493
this.server.emit("flush", this, this.writeBuffer);
492494
const wbuf = this.writeBuffer;
493495
this.writeBuffer = [];
494-
this.sentCallbackFn.push(this.packetsFn);
495-
this.packetsFn = [];
496+
497+
if (this.packetsFn.length) {
498+
this.sentCallbackFn.push(this.packetsFn);
499+
this.packetsFn = [];
500+
} else {
501+
this.sentCallbackFn.push(null);
502+
}
503+
496504
this.transport.send(wbuf);
497505
this.emit("drain");
498506
this.server.emit("drain", this);

0 commit comments

Comments
 (0)