Test name | Executions per second |
---|---|
Array.from + join + fromCodePoint | 272.6 Ops/sec |
Array.from + join + fromCharCode | 274.9 Ops/sec |
Array.reduce + fromCodePoint | 1155.3 Ops/sec |
Array.reduce + fromCharCode | 1122.5 Ops/sec |
String concatenation + fromCodePoint | 1423.8 Ops/sec |
String concatenation + fromCharCode | 1434.4 Ops/sec |
Function.apply + Array.reduce+ fromCodePoint (chunked) | 532.2 Ops/sec |
Function.apply + Array.reduce + fromCharCode (chunked) | 1485.3 Ops/sec |
Function.apply + String concatenation + fromCodePoint (chunked) | 828.0 Ops/sec |
Function.apply + String concatenation + fromCharCode (chunked) | 1661.4 Ops/sec |
Function.apply + Array.reduce+ fromCodePoint (small chunks) | 786.5 Ops/sec |
Function.apply + Array.reduce + fromCharCode (small chunks) | 1554.6 Ops/sec |
Function.apply + String concatenation + fromCodePoint (small chunks) | 774.1 Ops/sec |
Function.apply + String concatenation + fromCharCode (small chunks) | 1601.5 Ops/sec |
utf-16 TextDecoder | 4179.2 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));