Vector implementations

JavaScript performance comparison

Test case created by stylemistake

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const LENGTH = 100;
  
  class VectorComponentArray {
    constructor(x, y, z) {
      this.c = [x, y, z];
    }
  }
  
  class VectorComponentArray2 {
    constructor(c) {
      this.c = c;
    }
  }
  
  class VectorProps {
    constructor(x, y, z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }
  }
  
  function VectorProps2(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  
  class VectorArray extends Array {
    constructor(x, y, z) {
      super(x, y, z);
    }
  }
  
  class VectorArray2 extends Array {
    constructor(x, y, z) {
      super(3);
      this[0] = x;
      this[1] = y;
      this[2] = z;
    }
  }
  
  const VectorFloat32Array = (x, y, z) => {
    const vec = new Float32Array(3);
    vec[0] = x;
    vec[1] = y;
    vec[2] = z;
    return vec;
  };
  
  const VectorFloat64Array = (x, y, z) => {
    const vec = new Float64Array(3);
    vec[0] = x;
    vec[1] = y;
    vec[2] = z;
    return vec;
  };
  
  const VectorFloat64Array2 = (x, y, z) => {
    return new Float64Array(new Array(x, y, z));
  };
  

};
</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
VectorComponentArray
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = new VectorComponentArray(f, f, f);
  vec.c[0] = vec.c[1] + vec.c[2];
  vec.c[1] = vec.c[0] + vec.c[2];
  vec.c[2] = vec.c[0] + vec.c[1];
}
pending…
VectorComponentArray2
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = new VectorComponentArray2([f, f, f]);
  vec.c[0] = vec.c[1] + vec.c[2];
  vec.c[1] = vec.c[0] + vec.c[2];
  vec.c[2] = vec.c[0] + vec.c[1];
}
pending…
VectorProps
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = new VectorProps(f, f, f);
  vec.x = vec.y + vec.z;
  vec.y = vec.x + vec.z;
  vec.z = vec.x + vec.y;
}
pending…
VectorProps2
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = new VectorProps2(f, f, f);
  vec.x = vec.y + vec.z;
  vec.y = vec.x + vec.z;
  vec.z = vec.x + vec.y;
}
pending…
VectorArray
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = new VectorArray(f, f, f);
  vec[0] = vec[1] + vec[2];
  vec[1] = vec[0] + vec[2];
  vec[2] = vec[0] + vec[1];
}
pending…
VectorArray2
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = new VectorArray2(f, f, f);
  vec[0] = vec[1] + vec[2];
  vec[1] = vec[0] + vec[2];
  vec[2] = vec[0] + vec[1];
}
pending…
VectorFloat32Array
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = VectorFloat32Array(f, f, f);
  vec[0] = vec[1] + vec[2];
  vec[1] = vec[0] + vec[2];
  vec[2] = vec[0] + vec[1];
}
pending…
VectorFloat64Array
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = VectorFloat64Array(f, f, f);
  vec[0] = vec[1] + vec[2];
  vec[1] = vec[0] + vec[2];
  vec[2] = vec[0] + vec[1];
}
pending…
VectorFloat64Array2
let vec;
for (let i = 0; i < LENGTH; i++) {
  const f = i + 0.1;
  vec = VectorFloat64Array2(f, f, f);
  vec[0] = vec[1] + vec[2];
  vec[1] = vec[0] + vec[2];
  vec[2] = vec[0] + vec[1];
}
pending…

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

Compare results of other browsers

0 Comments