Minor optimization to set buffer size appropriately

This avoids unnecessary allocations/reallocations while decoding a message by pre-sizing the buffer to the amount we know it will require. I feel like there's probably a better way to handle this, but this works for now.
This commit is contained in:
2020-02-10 21:36:13 -06:00
parent 5a55c56ac1
commit ed99d411b2
8 changed files with 48 additions and 9 deletions

View File

@ -3,8 +3,14 @@
const SmartBuffer = require('smart-buffer').SmartBuffer;
exports.SLMessage = class SLMessage extends SmartBuffer {
constructor(senderId, messageId) {
super();
constructor(senderId, messageId, size) {
var options;
if (size) {
options = {
size: size,
};
}
super(options);
if (typeof senderId === 'number' || typeof senderId === 'undefined') {
this.writeUInt16LE(senderId || 0);