Array vs linked list (iteration)

Benchmark created on


Setup

function createListForValues(values) {
    const len = values.length;
    let previous = {
        value: values[0],
        previous: undefined,
        next: undefined
    };
    const list = {
        head: previous,
        tail: undefined,
        size: len
    };
    for (let i = 1; i < len; ++i) {
        previous = previous.next = {
            value: values[i],
            previous: previous,
            next: undefined
        };
    }
    list.tail = previous;
    return list;
}
function sumList(list) {
    let node = list.head;
    let sum = 0;
    while (node) {
        sum += node.value;
        node = node.next;
    }
    return sum;
}
function sumListSized(list) {
    const len = list.size;
    let node = list.head;
    let sum = 0;
    for (let i = 0; i < len; ++i) {
        sum += node.value;
        node = node.next;
    }
    return sum;
}
function sumArray(array) {
    const len = array.length;
    let sum = 0;
    for (let i = 0; i < len; ++i)
        sum += array[i];
    return sum;
}
const N = 1000;
const array = Array(N);
for (let i = 0; i < N; ++i) {
    array[i] = ~~(Math.random() * 100);
}
const list = createListForValues(array);

Test runner

Ready to run.

Testing in
TestOps/sec
Array
sumArray(array);
ready
Linked list unsized
sumList(list);
ready
Linked list sized
sumListSized(list);
ready

Revisions

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