diff --git a/lib/connection.js b/lib/connection.js index 22a73332..19cbcaa8 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -560,7 +560,10 @@ C.sendMessage = function(channel, var allLen = methodHeaderLen + bodyLen; if (allLen < SINGLE_CHUNK_THRESHOLD) { - var all = Buffer.alloc(allLen); + // Use `allocUnsafe` to avoid excessive allocations and CPU usage + // from zeroing. The returned Buffer is not zeroed and so must be + // completely filled to be used safely. + var all = Buffer.allocUnsafe(allLen); var offset = mframe.copy(all, 0); offset += pframe.copy(all, offset); @@ -570,7 +573,10 @@ C.sendMessage = function(channel, } else { if (methodHeaderLen < SINGLE_CHUNK_THRESHOLD) { - var both = Buffer.alloc(methodHeaderLen); + // Use `allocUnsafe` to avoid excessive allocations and CPU usage + // from zeroing. The returned Buffer is not zeroed and so must be + // completely filled to be used safely. + var both = Buffer.allocUnsafe(methodHeaderLen); var offset = mframe.copy(both, 0); pframe.copy(both, offset); buffer.write(both);