From 66add6defc36229d80a6b1ad27918a80e6d1c57e Mon Sep 17 00:00:00 2001 From: brianc Date: Wed, 29 Dec 2010 19:04:33 -0600 Subject: [PATCH] shortcut the join method for single buffer writers --- lib/writer.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/writer.js b/lib/writer.js index cd728079..a28d575b 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -17,9 +17,12 @@ p.addInt16 = function(val, front) { }; p.getByteLength = function(initial) { - return this.buffers.reduce(function(previous, current){ - return previous + current.length; - },initial || 0); + var totalBufferLength = 0; + var buffers = this.buffers; + for(var i = 0, len = buffers.length; i < len; i++) { + totalBufferLength += buffers[i].length; + } + return totalBufferLength; }; p.addInt32 = function(val, first) { @@ -44,10 +47,13 @@ p.addChar = function(char, first) { }; p.join = function() { + var length = this.buffers.length; + if(length===1) { + return this.buffers[0] + } var result = Buffer(this.getByteLength()); var index = 0; var buffers = this.buffers; - var length = this.buffers.length; for(var i = 0; i < length; i ++) { var buffer = buffers[i]; buffer.copy(result, index, 0);