linked list vs array

JavaScript performance comparison

Test case created by jzohdi

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
Linked List Add
class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}
class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }
  addToHead(ele) {
    const n = new Node(ele);
    n.next = this.head;
    this.head = n;
    this.size++;
  }
}
const list = new LinkedList();
let testSize = 1000
for (let x = 0; x < testSize; x++) {
  list.addToHead(x);
}
pending…
array add and remove
const arr = [];
let testSize = 1000;
for (let x = 0; x < testSize; x++) {
  arr.unshift(x);
}
const getMidValue = (value) => {
  return Math.floor(value / 2);
};
for (let x = testSize - 1; x >= 0; x--) {
  arr.splice(getMidValue(arr.length), 1);
}
pending…
array Add
const arr = [];
let testSize = 1000
for (let x = 0; x < testSize; x++) {
  arr.unshift(x);
}
pending…
linked list add and remove
class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}
class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }
  addToHead(ele) {
    const n = new Node(ele);
    n.next = this.head;
    this.head = n;
    this.size++;
  }
  removeIndex(i) {
    if (i >= this.size) {
      throw new Error("Remove index out of bounds");
    }
    if (this.size === 1) {
      this.head = null;
      this.size--;
      return;
    }
    let x = 0;
    let curr = this.head;
    while (x < i - 1) {
      curr = curr.next;
      x++;
    }
    curr.next = curr.next.next;
    this.size--;
  }
}
const list = new LinkedList();
let testSize = 1000
for (let x = 0; x < testSize; x++) {
  list.addToHead(x);
}
const getMidValue = (value) => {
  return Math.floor(value / 2);
};
for (let x = testSize - 1; x >= 0; x--) {
  list.removeIndex(getMidValue(list.size));
}
pending…

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

0 Comments