Fast Typed Arrays

JavaScript performance comparison

Test case created by Ben Vanik

Info

In a comment on http://blog.n01se.net/?p=248 mraleph mentions that monomorphic functions should be faster than polymorphic functions when using typed arrays. The idea is that v8 can optimize typed arrays in the case that a function ONLY uses them. If a function is called with a js array at all, the function will not be optimized.

The prep code 'initializes' the various fill functions by calling them on different array types. The first two tests try filling both js and typed arrays using the same function that was called first with a js array and then with a typed array. The second two tests try using a function called with the reverse - first a typed array then a js array. Finally, the last two tests call functions that have only been called with their respective array types.

Preparation code

<script>
  function fillArray(a, w, h) {
   var n = 0;
   for (var y = 0; y < h; y++) {
    for (var x = 0; x < w; x++) {
     a[n++] = x % 128 + 1;
     a[n++] = x % 128 + 2;
     a[n++] = x % 128 + 3;
     a[n++] = x % 128 + 4;
    }
   }
  };
 
  function fillArray2(a, w, h) {
   var n = 0;
   for (var y = 0; y < h; y++) {
    for (var x = 0; x < w; x++) {
     a[n++] = x % 128 + 1;
     a[n++] = x % 128 + 2;
     a[n++] = x % 128 + 3;
     a[n++] = x % 128 + 4;
    }
   }
  };
 
  function fillArrayJS(a, w, h) {
   var n = 0;
   for (var y = 0; y < h; y++) {
    for (var x = 0; x < w; x++) {
     a[n++] = x % 128 + 1;
     a[n++] = x % 128 + 2;
     a[n++] = x % 128 + 3;
     a[n++] = x % 128 + 4;
    }
   }
  };
 
  function fillArrayUint8(a, w, h) {
   var n = 0;
   for (var y = 0; y < h; y++) {
    for (var x = 0; x < w; x++) {
     a[n++] = x % 128 + 1;
     a[n++] = x % 128 + 2;
     a[n++] = x % 128 + 3;
     a[n++] = x % 128 + 4;
    }
   }
  };
 
  var s_jsarray = [];
  s_jsarray.length = 1024 * 1024 * 4;
  for (var n = 0; n < s_jsarray.length; n++) {
   s_jsarray[n] = 0;
  }
  var s_tyarray = new Uint8Array(1024 * 1024 * 4);
 
  var s_jsarray1 = [0, 0, 0, 0];
  var s_tyarray1 = new Uint8Array(4);
  fillArray(s_jsarray1, 1, 1);
  fillArray(s_tyarray1, 1, 1);
  fillArray2(s_tyarray1, 1, 1);
  fillArray2(s_jsarray1, 1, 1);
  fillArrayJS(s_jsarray1, 1, 1);
  fillArrayUint8(s_tyarray1, 1, 1);
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Polymorphic JS
fillArray(s_jsarray, 1024, 1024);
pending…
Polymorphic Typed
fillArray(s_tyarray, 1024, 1024);
pending…
Polymorphic JS Reversed
fillArray2(s_jsarray, 1024, 1024);
pending…
Polymorphic Typed Reversed
fillArray2(s_tyarray, 1024, 1024);
pending…
Monomorphic JS
fillArrayJS(s_jsarray, 1024, 1024);
pending…
Monomorphic Typed
fillArrayUint8(s_tyarray, 1024, 1024);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment