Script: early exit in chunk.reduce

This commit is contained in:
Daniel Cousens 2014-06-13 09:36:11 +10:00
parent be29f50457
commit de1571647a

View file

@ -43,17 +43,11 @@ Script.fromChunks = function(chunks) {
assert(Array.isArray(chunks), 'Expected Array, got ' + chunks) assert(Array.isArray(chunks), 'Expected Array, got ' + chunks)
var bufferSize = chunks.reduce(function(accum, chunk) { var bufferSize = chunks.reduce(function(accum, chunk) {
var chunkSize
if (Buffer.isBuffer(chunk)) { if (Buffer.isBuffer(chunk)) {
chunkSize = bufferutils.pushDataSize(chunk.length) + chunk.length return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
} else {
chunkSize = 1
} }
return accum + chunkSize return accum + 1
}, 0.0) }, 0.0)
var buffer = new Buffer(bufferSize) var buffer = new Buffer(bufferSize)
@ -67,8 +61,6 @@ Script.fromChunks = function(chunks) {
offset += chunk.length offset += chunk.length
} else { } else {
assert(typeof chunk == 'number')
buffer.writeUInt8(chunk, offset) buffer.writeUInt8(chunk, offset)
offset += 1 offset += 1
} }