TypedArray vs Array vs Obj Array

JavaScript performance comparison

Revision 4 of this test case created by Chandler Prall

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const pos_array = [];
  const size = 1000;
  const pos_vel_sum_float64 = new Float64Array(size * 3);
  while (pos_array.length < size) {
    pos_array.push(Math.random())
    pos_vel_sum_float64[pos_array.length - 1] = pos_array[pos_array.length - 1]
  }
  const pos_typed64 = Float64Array.from(pos_array);
  const sum_float64array = new Float64Array(size);
  const pos_typed32 = Float32Array.from(pos_array);
  const pos_obj = pos_array.map((val) => ({ val }))
  
  const vel_array = [];
  while (vel_array.length < size) {
    vel_array.push(Math.random())
    pos_vel_sum_float64[size + pos_array.length - 1] = pos_array[pos_array.length - 1]
  }
  const vel_typed64 = Float64Array.from(vel_array);
  const vel_typed32 = Float32Array.from(vel_array);
  const vel_obj = vel_array.map((val) => ({ val }))
  
  const sum_float32array = new Float32Array(size);
  const sum_float32over64array = new Float32Array(size);
  const sum_float64array = new Float64Array(size);
  const sum_float64over32array = new Float64Array(size);
  const sum_arrayofnumbers = new Array(size);
  const sum_arrayofobjects = new Array(size);

};
</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
Single Float64Array
for (var i=0; i<pos_typed64.length; i++) {
  const src1 = i;
  const src2 = i + size;
  const dest = size + size + i;
  pos_vel_sum_float64[dest] = pos_vel_sum_float64[src1] + pos_vel_sum_float64[src2];
}
pending…
Array of Objects
for (var i=0; i<pos_obj.length; i++) {
  sum_arrayofobjects[i] = { val: pos_obj[i].val + vel_obj[i].val };
}
pending…
Float64Array, summing over 32bit
for (var i=0; i<pos_typed64.length; i++) {
  sum_float64over32array[i] = pos_typed32[i] + vel_typed32[i];
}
pending…
Float64Array
for (var i=0; i<pos_typed64.length; i++) {
  sum_float64array[i] = pos_typed64[i] + vel_typed64[i];
}
pending…
Float32Array, summing over 64bit
for (var i=0; i<pos_typed32.length; i++) {
  sum_float32over64array[i] = pos_typed64[i] + vel_typed64[i];
}
pending…
Array of Numbers
for (var i=0; i<pos_array.length; i++) {
  sum_arrayofnumbers[i] = pos_array[i] + vel_array[i];
}
pending…
Float32Array
for (var i=0; i<pos_typed32.length; i++) {
  sum_float32array[i] = pos_typed32[i] + vel_typed32[i];
}
pending…

Revisions

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

0 Comments