Add a property to an array

JavaScript performance comparison

Test case created by Erik Corry

Preparation code

<script>
function plainArray() {
  var a = [];
  for (var i = 0; i < 10000; i++) {
    a[i] = i;
  }
  var sum = 0;
  for (var i = 0; i < a.length; i++) {
    sum += a[i];
  }
  return sum;
}
function arrayWithProperty() {
  var a = [];
  a.foo = 123;
  for (var i = 0; i < 10000; i++) {
    a[i] = i;
  }
  var sum = 0;
  for (var i = 0; i < a.length; i++) {
    sum += a[i];
  }
  return sum;
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Plain array
plainArray();
pending…
Array with property
arrayWithProperty();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment