Script Preparation code:
x
 
// needed to create bytes array
var encoder = new TextEncoder();
// string length, multiple of 4 - vary to get different results
var n = 8;
var arr = encoder.encode(Array((n / 4) | 0).fill('abcd'));
var decoder = new TextDecoder();
function decodeNative(arr) {
  return decoder.decode(arr);
}
function decoderJs(buffer) {
  var start = 0;
  var end = buffer.byteLength;
  if (end - start < 1) {
    return "";
  }
  var str = "";
  for (var i = start; i < end;) {
    var t = buffer[i++];
    if (t <= 0x7F) {
      str += String.fromCharCode(t);
    } else if (t >= 0xC0 && t < 0xE0) {
      str += String.fromCharCode((t & 0x1F) << 6 | buffer[i++] & 0x3F);
    } else if (t >= 0xE0 && t < 0xF0) {
      str += String.fromCharCode((t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F);
    } else if (t >= 0xF0) {
      var t2 = ((t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F) - 0x10000;
      str += String.fromCharCode(0xD800 + (t2 >> 10));
      str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
    }
  }
  return str;
}
Tests:
  • decodeNative

     
    decodeNative(arr)
  • decoderJs

     
    decoderJs(arr)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    decodeNative
    decoderJs

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0
Firefox 99 on Linux
View result in a separate tab
Test name Executions per second
decodeNative 6869118.5 Ops/sec
decoderJs 4343495.5 Ops/sec