linked list vs array

Benchmark created by jzohdi on


Test runner

Ready to run.

Testing in
TestOps/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);
}
ready
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);
}
ready
array Add
const arr = [];
let testSize = 1000
for (let x = 0; x < testSize; x++) {
  arr.unshift(x);
}
ready
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));
}
ready

Revisions

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