Linked List vs Array Push

JavaScript performance comparison

Revision 4 of this test case created by Jamie Hill

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var LinkedNode = function(item, prev, next) {
      this.item = item;
      this.prev = prev;
      this.next = next;
    };
   
    var LinkedList = function(item) {
      this.head = item ? new LinkedNode(item, null, null) : null;
      this.last = this.head;
      this.length = 0;
      this.current;
    };
   
    LinkedList.prototype.add = function( item ) {
      var node;
      if( this.head === null ) {
        node = this.head = new LinkedNode(item, null, null);
      } else {
        node = this.last.next = new LinkedNode(item, this.last, null);
      }
      this.length += 1;
      this.last = node;
    };
     
   
    var list = new LinkedList();
    var arry = [];
    var i = 0;
};
</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
Linked List Push
list.add(i++);
pending…
Array Push
arry.push(i++);
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