Big Array Vs Big Linked List

JavaScript performance comparison

Revision 3 of this test case created by Jeremy Diviney

Info

v2: Shrunk listSize from 10000000 to 100000 since the tests ran <1/second. Though perhaps a 10000000 array test is needed, but jsPerf is not the platform for that test.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var LLNode = function() {
   
        this.next = null;
        this.val = '';
   
        };
   
    var listSize = 100000;
   
    var ary = [];
    var ary2 = new Array(listSize);
};
</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
Array
for (var x = 0; x < listSize; x++) {
  ary.push({});
}
pending…
Linked list
var lastNode, firstNode;
for (var x = 0; x < listSize; x++) {
  if (x > 0) {
    lastNode.nextNode = (new LLNode());
    lastNode = lastNode.nextNode;
  } else {
    lastNode = new LLNode();
    firstNode = lastNode;
  }
}
pending…
Array pre initialized
for (var x = 0; x < listSize; x++) {
  ary2[x] = new LLNode();
}
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