floatarray vs inline storage

Benchmark created on


Setup

class VectorArray {
	data = new Float32Array(2)
	dimension = 2
	
	get x() { return this.data[0] }
	set x(value) { this.data[0] = value }
	
	get y() { return this.data[1] }
	set y(value) { this.data[1] = value }
	
	
  /** @param {number} scalar */
  mul(scalar) {
    for (let i = 0; i < this.dimension; i++) {
      this.data[i] *= scalar
    }

    return this
  }

  /** @param {VFXVector} other */
  add(other) {
    for (let i = 0; i < this.dimension; i++) {
      this.data[i] += other.data[i]
    }

    return this
  }

  /** @param {VFXVector} other */
  sub(other) {
    for (let i = 0; i < this.dimension; i++) {
      this.data[i] -= other.data[i]
    }

    return this
  }
	
	constructor(x, y) {
		this.data[0] = x
		this.data[1] = y
	}
}

class VectorArrayDestructoring {
	data
	dimension = 2
	
	get x() { return this.data[0] }
	set x(value) { this.data[0] = value }
	
	get y() { return this.data[1] }
	set y(value) { this.data[1] = value }
	
	
  /** @param {number} scalar */
  mul(scalar) {
    for (let i = 0; i < this.dimension; i++) {
      this.data[i] *= scalar
    }

    return this
  }

  /** @param {VFXVector} other */
  add(other) {
    for (let i = 0; i < this.dimension; i++) {
      this.data[i] += other.data[i]
    }

    return this
  }

  /** @param {VFXVector} other */
  sub(other) {
    for (let i = 0; i < this.dimension; i++) {
      this.data[i] -= other.data[i]
    }

    return this
  }
	
	constructor(...data) {
		this.data = new Float32Array(data)
	}
}

class VectorProperties {
	x; y;
	
	
  /** @param {number} scalar */
  mul(scalar) {
    this.x *= scalar
    this.y *= scalar

    return this
  }

  /** @param {VFXVector} other */
  add(other) {
    this.x += other.x
    this.y += other.y

    return this
  }

  /** @param {VFXVector} other */
  sub(other) {
    this.x -= other.x
    this.y -= other.y

    return this
  }
	
	constructor(x, y) {
		this.x = x
		this.y = y
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
with float32 storage
const Vector = VectorArray


v1 = new Vector(1, 2)
v2 = new Vector(3, 4)

v1.add(v2).mul(3).sub(v2)

const result = v1.x + v1.y + v2.x + v2.y
console.log(result)
ready
with float32 storage and destructoring
const Vector = VectorArrayDestructoring


v1 = new Vector(1, 2)
v2 = new Vector(3, 4)

v1.add(v2).mul(3).sub(v2)

const result = v1.x + v1.y + v2.x + v2.y
console.log(result)
ready
with property storage
const Vector = VectorProperties


v1 = new Vector(1, 2)
v2 = new Vector(3, 4)

v1.add(v2).mul(3).sub(v2)

const result = v1.x + v1.y + v2.x + v2.y
console.log(result)
ready

Revisions

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