Array vs linked list (creation)

Benchmark created on


Setup

function buildArrayPush(data) {
    const len = data.length;
    const result = [];
    for (let i = 0; i < len; ++i) {
        result.push(data[i]);
    }
    return result;
}
function buildArraySet(data) {
    const len = data.length;
    const result = Array(len);
    for (let i = 0; i < len; ++i) {
        result[i] = (data[i]);
    }
    return result;
}
function buildListAppend(data) {
    const len = data.length;
    const head = {
        value: data[0],
        previous: undefined,
        next: undefined
    };
    const result = {
        head: head,
        tail: head,
        size: 1
    };
    let tail = result.tail;
    for (let i = 1; i < len; ++i) {
        const node = {
            value: data[i],
            previous: tail,
            next: undefined
        };
        result.tail = tail = tail.next = node;
        result.size++;
    }
    return result;
}
const N = 1000;
const data = Array(N);
let array;
let list;

for (let i = 0; i < N; ++i) {
    data[i] = ~~(Math.random() * 100);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array push
array = buildArrayPush(data);
ready
Array set
array = buildArraySet(data);
ready
List append
list = buildListAppend(data);
ready

Revisions

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