javascript - Are there, currently, noticeable performance advantages in using Buffer / Uint8Array instead of plain arrays? -
i believe, nowadays, javascript engines such v8 able detect when array has numeric values in 0-255
range and, thus, store unboxed uint8 array. such, 1 expect they're efficient typed counterparts. having single array type across codebase is, though, more convenient having many.
in 2017, there still noticeable performance advantage in using buffer
/uint8array
s rather plain arrays?
see below code used performance testing using runkit. indicates buffer
slower, consistent other historic data.
other related data points:
- this jsperf
- this other jsperf
-
//setup //const inumtests = 1000; const inumtests = 1000; let arrresults = []; function randomchar() { var chars = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; return chars[math.round(math.random() * (chars.length - 1))]; } function singletest() { //buffer const t0 = process.hrtime(); let buf = buffer.from(randomchar()); //recommended initialize .from() (var = 1; < inumtests; ++i) { buf.write(randomchar()); } var soutput = buf.tostring(); const arrbuff = process.hrtime(t0); //array const t1 = process.hrtime(); let chars = []; (var = 0; < inumtests; ++i) { chars.push(randomchar()); } var soutput = chars.join(''); const arrarray = process.hrtime(t1); //delta const timebuff = arrbuff[0] * 1e9 + arrbuff[1]; const timearray = arrarray[0] * 1e9 + arrarray[1]; return (timebuff - timearray); } (var = 0; < inumtests; i++) { arrresults.push(singletest()); } var sum = arrresults.reduce(function(acc, val) { return acc + val; }, 0); console.log(sum/inumtests); // average difference. got 146929.598 means timebuff larger on average.
Comments
Post a Comment