utf-8 decoding (v2)

Revision 2 of this benchmark created on


Description

test

Setup

const decoder = new TextDecoder('utf-8');
// const strLen = 25e6;
const strLen = 100;
const data = new Uint8Array(new Array(strLen).fill(65));

const customDecode = function (octets) {
  var string = "";
  var i = 0;
  while (i < octets.length) {
    var octet = octets[i];
    var bytesNeeded = 0;
    var codePoint = 0;
    if (octet <= 0x7F) {
      bytesNeeded = 0;
      codePoint = octet & 0xFF;
    } else if (octet <= 0xDF) {
      bytesNeeded = 1;
      codePoint = octet & 0x1F;
    } else if (octet <= 0xEF) {
      bytesNeeded = 2;
      codePoint = octet & 0x0F;
    } else if (octet <= 0xF4) {
      bytesNeeded = 3;
      codePoint = octet & 0x07;
    }
    if (octets.length - i - bytesNeeded > 0) {
      var k = 0;
      while (k < bytesNeeded) {
        octet = octets[i + k + 1];
        codePoint = (codePoint << 6) | (octet & 0x3F);
        k += 1;
      }
    } else {
      codePoint = 0xFFFD;
      bytesNeeded = octets.length - i;
    }
    string += String.fromCodePoint(codePoint);
    i += bytesNeeded + 1;
  }
  return string
};

Test runner

Ready to run.

Testing in
TestOps/sec
native decoder
decoder.decode(data);
ready
custom decoder
customDecode(data);
ready
native decoder + subarray
decoder.decode(data.subarray(0, data.length-1));
ready
custom decoder + subarray
customDecode(data.subarray(0, data.length-1));
ready

Revisions

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