Loop speed for typed arrays (v7)

Revision 7 of this benchmark created on


Description

Here we will explore the speed of looping through a flattened typed array vs an array of Objects with polymorphic key types (AKA, a standard POJO)

Setup

var Particle = function() {
      if (!(this instanceof Particle)) return new Particle
      this.x = 1.0
      this.y = 2.0
    }
    
    var count = 200000
    var particlesPOJO = []
    var particlesTyped = new Float32Array(count * 2)
    
    for (var i = 0; i < count; ++i) {
      particlesPOJO.push(Particle())
    }
    
    for (var i = 0; i < count; i += 2) {
      particlesTyped[i] = 1.0
      particlesTyped[i + 1] = 2.0
    }

Test runner

Ready to run.

Testing in
TestOps/sec
array of POJOs
for (var i = 0; i < particlesPOJO.length; ++i) {
  particlesPOJO[i].x++
}
ready
typed array
for (var i = 0; i < particlesTyped.length; i += 1) {
  particlesTyped[i]++
}
ready
for in array of POJOs
for (var i in particlesPOJO) {
  particlesPOJO[i].x++
}
ready
for in type array
for (var i in particlesTyped) {
  particlesTyped[i]++
}
ready

Revisions

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