Skip to content

Instantly share code, notes, and snippets.

@ledbit
Last active February 14, 2022 12:29
Show Gist options
  • Save ledbit/fa90e5fbefd8076f4395aa6d8e410aa6 to your computer and use it in GitHub Desktop.
Save ledbit/fa90e5fbefd8076f4395aa6d8e410aa6 to your computer and use it in GitHub Desktop.
nodejs buffer string write performance
'use strict';
function writeUTF8ToBuf(str, buf, off=0) {
for (let i=0; i < str.length; i++) {
const charcode = str.charCodeAt(i);
if (charcode < 0x80) buf[off++] = charcode;
else if (charcode < 0x800) {
buf[off++] = 0xc0 | (charcode >> 6);
buf[off++] = 0x80 | (charcode & 0x3f);
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
buf[off++] = 0xe0 | (charcode >> 12);
buf[off++] = 0x80 | ((charcode>>6) & 0x3f);
buf[off++] = 0x80 | (charcode & 0x3f);
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff)<<10) | (str.charCodeAt(i) & 0x3ff));
buf[off++] = 0xf0 | (charcode >>18);
buf[off++] = 0x80 | ((charcode>>12) & 0x3f);
buf[off++] = 0x80 | ((charcode>>6) & 0x3f);
buf[off++] = 0x80 | (charcode & 0x3f);
}
}
return off;
}
const buf = Buffer.alloc(4096);
const count = 1e7;
console.log('StrLen,writeUTF8ToBuf,Buffer.utf8Write,Buffer.write')
for(let k=0; k<11; k++) {
const str = 'a'.repeat(Math.pow(2, k));
const times = [str.length];
let start = Date.now();
for (let i = 0; i < count; ++i){
writeUTF8ToBuf(str, buf);
}
times.push(Date.now() - start);
start = Date.now();
for (let i = 0; i < count; ++i){
buf.utf8Write(str);
}
times.push(Date.now() - start);
start = Date.now();
for (let i = 0; i < count; ++i){
buf.write(str);
}
times.push(Date.now() - start);
console.log(times.join(','))
}
@ledbit
Copy link
Author

ledbit commented Apr 5, 2021

Performance comparison on different Node Versions - you can clearly see the regression, especially on writing shorter (len<32) strings.

Node 14.15.1 (and ~15.13.0)

StrLen,writeUTF8ToBuf,Buffer.utf8Write,Buffer.write
1,66,1266,1353
2,84,1281,1312
4,141,1286,1427
8,176,1330,1501
16,483,1256,1468
32,1004,1313,1638
64,2079,1331,1593
128,3816,1370,1502
256,7569,1362,1512
512,15522,1454,1725
1024,31060,1728,1858

Node 12.18.4

StrLen,writeUTF8ToBuf,Buffer.utf8Write,Buffer.write
1,46,699,783
2,80,663,708
4,128,625,804
8,191,634,790
16,512,732,925
32,1047,746,879
64,2018,853,1057
128,3949,790,958
256,8090,905,1055
512,17109,1001,1111
1024,36063,1121,1439

Node 10.24.0

StrLen,writeUTF8ToBuf,Buffer.utf8Write,Buffer.write
1,50,692,875
2,83,695,842
4,102,722,879
8,201,753,899
16,675,848,1044
32,1339,998,1226
64,2546,1183,1263
128,4882,1516,1786
256,9870,2354,2552
512,22166,4333,4071
1024,40880,7926,7789

@Uzlopak
Copy link

Uzlopak commented Feb 14, 2022

Used your gist for mongodb/js-bson#490

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