ArrayBuffer to String Apply Performance

JavaScript performance comparison

Revision 2 of this test case created by Justin

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var bigData = [];
  for (var i = 0; i < 512 * 1024; i++) {
      bigData.push(i % 256);
  }
  var bigArrayBuffer = new Uint8Array(bigData).buffer;
  
  var arrayBufferSubarrayToString = function(ab, chunk) {
          var len = ab.byteLength;
          var u8 = new Uint8Array(ab);
          if (len < chunk) {
              return String.fromCharCode.apply(null, u8);
          } else {
              var k = 0,
                  r = "";
              while (k < len) {
                  // slice is clamped, inclusive of startIndex, exclusive of lastIndex
                  r += String.fromCharCode.apply(null, u8.subarray(k, k + chunk));
                  k += chunk;
              }
              return r;
          }
      };
  
  var arrayBufferSliceToString = function(ab, chunk) {
          var len = ab.byteLength;
          if (len < chunk) {
              return String.fromCharCode.apply(null, new Uint8Array(ab));
          } else {
              var k = 0,
                  r = "";
              while (k < len) {
                  // slice is clamped, inclusive of startIndex, exclusive of lastIndex
                  r += String.fromCharCode.apply(null, new Uint8Array(ab.slice(k, k + chunk)));
                  k += chunk;
              }
              return r;
          }
      };
  
  var iterateABtoString = function(ab) {
      var u8 = new Uint8Array(ab);
      var str = "";
      for (var i = 0; i < u8.length; i++) {
          str += String.fromCharCode(u8[i]);
      }
      return str;
  };
  
  var setAndJoin = function(ab) {
      var u8 = new Uint8Array(ab);
      var str_array = [];
      for (var i = 0; i < u8.length; i++) {
          str_array[i] = String.fromCharCode(u8[i]);
      }
      return str_array.join("");
  };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
32 byte Subarray
arrayBufferSubarrayToString(bigArrayBuffer, 32);
pending…
8 kB Subarray
arrayBufferSubarrayToString(bigArrayBuffer, 8192);
pending…
32 kB Subarray
arrayBufferSubarrayToString(bigArrayBuffer, 32768);
pending…
64 kB Subarray
arrayBufferSubarrayToString(bigArrayBuffer, 65536);
pending…
Iterate and Append
iterateABtoString(bigArrayBuffer);
pending…
Set and Join
setAndJoin(bigArrayBuffer);
pending…
32 byte Slice
arrayBufferSliceToString(bigArrayBuffer, 32);
pending…
8 kb Slice
arrayBufferSliceToString(bigArrayBuffer, 8192);
pending…
32 kB Slice
arrayBufferSliceToString(bigArrayBuffer, 32768);
pending…
64 kB Slice
arrayBufferSliceToString(bigArrayBuffer, 65536);
pending…

Compare results of other browsers

Revisions

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

0 Comments