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(','))
}
@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