Last active
February 14, 2022 12:29
-
-
Save ledbit/fa90e5fbefd8076f4395aa6d8e410aa6 to your computer and use it in GitHub Desktop.
nodejs buffer string write performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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(',')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)
Node 12.18.4
Node 10.24.0