push string to array, generate string by join all data with comma.
write int(16bit) to buffer, generate string by buffer.toString('base64').
batch collect data, and send them to another server or process, with high speed and large quantity.
node version : v4.1.2 platform: Intel I7, 16G DDR3, Ubuntu x64
console.time('section 1');
var ary1 = [];
for(let i = 0 ; i < N; i++) {
ary1.push('123');
}
var str = ary1.join(',');
console.timeEnd('section 1');
console.time('section 2');
var buf = new Buffer(10000000);
for(let i = 0 ; i < N; i++) {
buf.writeInt16LE(123, i * 2);
}
var encode = buf.slice(0, buf.length).toString('base64');
console.timeEnd('section 2');
result:
# N : 10K
section 1: 3ms
section 2: 4ms
# N : 100K
section 1: 7ms
section 2: 5ms
# N : 1M
section 1: 101ms
section 2: 20ms
# N : 10M
section 1: 1282ms
section 2: 159ms
buffer solution is more effiency than array solution when data scale large enough.
for common usage, array is convenience and efficiency.