array vs linked list: reverse List

Benchmark publishedon

Setup

const SIZE = 20000;

const baseArr = Array.from({ length: SIZE }, (_, i) => i);

function buildSingly(arr) {
  let head = { val: arr[0], next: null };
  let curr = head;

  for (let i = 1; i < arr.length; i++) {
    curr.next = { val: arr[i], next: null };
    curr = curr.next;
  }
  return head;
}

function buildDoubly(arr) {
  let head = { val: arr[0], next: null, prev: null };
  let curr = head;

  for (let i = 1; i < arr.length; i++) {
    const node = { val: arr[i], next: null, prev: curr };
    curr.next = node;
    curr = node;
  }
  return head;
}

Test Runner

Initializing...

Testing in
Test CaseOps/sec
arr
const arr = [...baseArr];

arr.reverse();
ready
singly Llist
let head = buildSingly(baseArr);

let prev = null;
let curr = head;

while (curr) {
  const next = curr.next;
  curr.next = prev;
  prev = curr;
  curr = next;
}

head = prev;
ready
double Llist
let head = buildSingly(baseArr);

let prev = null;
let curr = head;

while (curr) {
  const next = curr.next;
  curr.next = prev;
  prev = curr;
  curr = next;
}

head = prev;
ready

Revisions

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

Revision 1
publishedon