Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Last active June 22, 2020 12:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chunlin-Li/38d07ad2453c82c6e2f3 to your computer and use it in GitHub Desktop.
Save Chunlin-Li/38d07ad2453c82c6e2f3 to your computer and use it in GitHub Desktop.
node js / javascript performance: buffer vs string

performance comparison:

section1 :

push string to array, generate string by join all data with comma.

section2 :

write int(16bit) to buffer, generate string by buffer.toString('base64').

background :

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

conclusion:

buffer solution is more effiency than array solution when data scale large enough.
for common usage, array is convenience and efficiency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment