Array.slice() vs TypedArray.subarray()

JavaScript performance comparison

Revision 2 of this test case created by Moncader

Info

If you're doing a lot of slicing vs accesses, is it worth it to use a TypedArray instead of a regular Array?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var length = 1000;
    var start = 200;
    var end = 300;
   
    var num;
   
    var typed64 = new Float64Array(length);
    var typed32 = new Float32Array(length);
    var typedu8 = new Uint8Array(length);
    var array = new Array(length);
   
    for (var i = 0; i < length; i++) {
      num = Math.random() * 0xFF;
      typed64[i] = num;
      typed32[i] = num;
      typedu8[i] = num | 0;
      array[i] = num;
    }
};
</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
Array.slice()
array.slice(start, end);
pending…
Float64Array.subarray()
typed64.subarray(start, end);
pending…
Float32Array.subarray()
typed32.subarray(start, end);
pending…
Uint8Array.subarray
typedu8.subarray(start, end);
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