Test name | Executions per second |
---|---|
Array.from + join + fromCodePoint | 47.0 Ops/sec |
Array.from + join + fromCharCode | 43.7 Ops/sec |
Array.reduce + fromCodePoint | 62.6 Ops/sec |
Array.reduce + fromCharCode | 62.9 Ops/sec |
String concatenation + fromCodePoint | 58.2 Ops/sec |
String concatenation + fromCharCode | 60.0 Ops/sec |
Function.apply + Array.reduce+ fromCodePoint (chunked) | 553.7 Ops/sec |
Function.apply + Array.reduce + fromCharCode (chunked) | 950.6 Ops/sec |
Function.apply + String concatenation + fromCodePoint (chunked) | 517.6 Ops/sec |
Function.apply + String concatenation + fromCharCode (chunked) | 868.5 Ops/sec |
Function.apply + Array.reduce+ fromCodePoint (small chunks) | 399.1 Ops/sec |
Function.apply + Array.reduce + fromCharCode (small chunks) | 543.9 Ops/sec |
Function.apply + String concatenation + fromCodePoint (small chunks) | 372.8 Ops/sec |
Function.apply + String concatenation + fromCharCode (small chunks) | 542.3 Ops/sec |
utf-16 TextDecoder | 1745.5 Ops/sec |
function get1024Bytes(n) {
const bytes = new Uint8Array(n * 1024);
for (let i = 0; i < bytes.length; i += 1) {
bytes[i] = Math.random() * 0xff;
}
return bytes;
}
const bytes = get1024Bytes(128);
const utf16decoder = new TextDecoder('utf-16');
// An example result:
// const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join("");
return Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return Array.from(bytes, (byte) =>
String.fromCharCode(byte),
).join("");
return bytes.reduce((s, byte) => s + String.fromCodePoint(byte), "");
return bytes.reduce((s, byte) => s + String.fromCharCode(byte), "");
let s = '';
for (const byte of bytes) {
s += String.fromCodePoint(byte);
}
return s;
let s = '';
for (const byte of bytes) {
s += String.fromCharCode(byte);
}
return s;
const QUANTUM = 32768;
const chunks = [];
for (let i = 0; i < bytes.length; i += QUANTUM) {
chunks.push(bytes.slice(i, i + QUANTUM));
}
return chunks.reduce((s, i) => s + String.fromCodePoint.apply(null, i), '');
const QUANTUM = 32768;
const chunks = [];
for (let i = 0; i < bytes.length; i += QUANTUM) {
chunks.push(bytes.slice(i, i + QUANTUM));
}
return chunks.reduce((s, i) => s + String.fromCharCode.apply(null, i), '');
const QUANTUM = 32768;
let s = '';
for (let i = 0; i < bytes.length; i += QUANTUM) {
s += String.fromCodePoint.apply(null, bytes.slice(i, i + QUANTUM));
}
return s;
const QUANTUM = 32768;
let s = '';
for (let i = 0; i < bytes.length; i += QUANTUM) {
s += String.fromCharCode.apply(null, bytes.slice(i, i + QUANTUM));
}
return s;
const QUANTUM = 128;
const chunks = [];
for (let i = 0; i < bytes.length; i += QUANTUM) {
chunks.push(bytes.slice(i, i + QUANTUM));
}
return chunks.reduce((s, i) => s + String.fromCodePoint.apply(null, i), '');
const QUANTUM = 128;
const chunks = [];
for (let i = 0; i < bytes.length; i += QUANTUM) {
chunks.push(bytes.slice(i, i + QUANTUM));
}
return chunks.reduce((s, i) => s + String.fromCharCode.apply(null, i), '');
const QUANTUM = 128;
let s = '';
for (let i = 0; i < bytes.length; i += QUANTUM) {
s += String.fromCodePoint.apply(null, bytes.slice(i, i + QUANTUM));
}
return s;
const QUANTUM = 128;
let s = '';
for (let i = 0; i < bytes.length; i += QUANTUM) {
s += String.fromCharCode.apply(null, bytes.slice(i, i + QUANTUM));
}
return s;
return utf16decoder.decode(new Uint16Array(bytes));