Array Getters vs Arrays

JavaScript performance comparison

Test case created by Kibigo!

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var array1 = [];
  var array2 = [];
  var array3 = [];
  var i;
  
  for (i = 0; i < 1000; i++) {
    array1[i] = array3[i] = 1;
    Object.defineProperty(array2, i, {get: function () {return 1}, enumerable: true});
  }
  Object.freeze(array3);

};
</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
Accessing normal arrays
array1[Math.floor(Math.random() * 1000)]
pending…
Accessing getter arrays
array2[Math.floor(Math.random() * 1000)]
pending…
Accessing frozen arrays
array3[Math.floor(Math.random() * 1000)]
pending…

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

0 Comments