TextDecoder vs String.fromCharCode

Benchmark created on


Setup

const testLength = 1000;

const getRandomCharCode = () => Math.floor(Math.random() * 26) + 97; // a-z letters
const bytes = Array.from({length: testLength}, () => getRandomCharCode());

const buffer = new Uint8Array(bytes);
const decoder = new TextDecoder(); // default 'utf-8' or 'utf8'

Test runner

Ready to run.

Testing in
TestOps/sec
300-700, String.fromCharCode, subarray
String.fromCharCode(...buffer.subarray(300, 700));
ready
300-310, String.fromCharCode, subarray
String.fromCharCode(...buffer.subarray(300, 310));
ready
300-700, TextDecoder, new uint8array
const slice = new Uint8Array(buffer.buffer, 300, 700 - 300);
decoder.decode(slice);
ready
300-310, TextDecoder, new uint8array
const slice = new Uint8Array(buffer.buffer, 300, 310 - 300);
decoder.decode(slice);
ready
300-700, TextDecoder, subarray
decoder.decode(buffer.subarray(300, 700))
ready
300-310, TextDecoder, subarray
decoder.decode(buffer.subarray(300, 310))
ready
300-310, String.fromCharCode, subarray
String.fromCharCode(...buffer.subarray(300, 350));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.