Array.slice() vs TypedArray.subarray() (v4)

Revision 4 of this benchmark created by kevin on


Description

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

Setup

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;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
typedu8.slice()
typedu8.slice(start, end);
ready
Float64Array.subarray()
typed64.subarray(start, end);
ready
Float32Array.subarray()
typed32.subarray(start, end);
ready
Uint8Array.subarray
typedu8.subarray(start, end);
ready

Revisions

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

  • Revision 1: published by Heather on
  • Revision 2: published by Moncader on
  • Revision 4: published by kevin on