Elements accessed via assigned variables vs spread vs index (v2)

Revision 2 of this benchmark created on


Description

Performance on matrix when elements accessed via assigned variables vs spread vs index

Setup

const matrix = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);

function detSpread (matrix) {
    const [a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33] = matrix;

    const b0 = a00 * a11 - a01 * a10;
    const b1 = a00 * a12 - a02 * a10;
    const b2 = a01 * a12 - a02 * a11;
    const b3 = a20 * a31 - a21 * a30;
    const b4 = a20 * a32 - a22 * a30;
    const b5 = a21 * a32 - a22 * a31;
    const b6 = a00 * b5 - a01 * b4 + a02 * b3;
    const b7 = a10 * b5 - a11 * b4 + a12 * b3;
    const b8 = a20 * b2 - a21 * b1 + a22 * b0;
    const b9 = a30 * b2 - a31 * b1 + a32 * b0;

    // Calculate the determinant
    return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9;
}

function detVars(a) {
  let a00 = a[0],
    a01 = a[1],
    a02 = a[2],
    a03 = a[3];
  let a10 = a[4],
    a11 = a[5],
    a12 = a[6],
    a13 = a[7];
  let a20 = a[8],
    a21 = a[9],
    a22 = a[10],
    a23 = a[11];
  let a30 = a[12],
    a31 = a[13],
    a32 = a[14],
    a33 = a[15];

  let b0 = a00 * a11 - a01 * a10;
  let b1 = a00 * a12 - a02 * a10;
  let b2 = a01 * a12 - a02 * a11;
  let b3 = a20 * a31 - a21 * a30;
  let b4 = a20 * a32 - a22 * a30;
  let b5 = a21 * a32 - a22 * a31;
  let b6 = a00 * b5 - a01 * b4 + a02 * b3;
  let b7 = a10 * b5 - a11 * b4 + a12 * b3;
  let b8 = a20 * b2 - a21 * b1 + a22 * b0;
  let b9 = a30 * b2 - a31 * b1 + a32 * b0;

  // Calculate the determinant
  return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9;
}

function detIndex (matrix) {
    const b0 = matrix[0] * matrix[5] - matrix[1] * matrix[4];
    const b1 = matrix[0] * matrix[6] - matrix[2] * matrix[4];
    const b2 = matrix[1] * matrix[6] - matrix[2] * matrix[5];
    const b3 = matrix[8] * matrix[13] - matrix[9] * matrix[12];
    const b4 = matrix[8] * matrix[14] - matrix[10] * matrix[12];
    const b5 = matrix[9] * matrix[14] - matrix[10] * matrix[13];
    const b6 = matrix[0] * b5 - matrix[1] * b4 + matrix[2] * b3;
    const b7 = matrix[4] * b5 - matrix[5] * b4 + matrix[6] * b3;
    const b8 = matrix[8] * b2 - matrix[9] * b1 + matrix[10] * b0;
    const b9 = matrix[12] * b2 - matrix[13] * b1 + matrix[14] * b0;

    // Calculate the determinant
    return matrix[7] * b6 - matrix[3] * b7 + matrix[15] * b8 - matrix[11] * b9;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Spread
detSpread(matrix);
ready
Index
detIndex(matrix)
ready
Variables
detVars(matrix);
ready

Revisions

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