Compare linked list loop

Benchmark created by Igor on


Test runner

Ready to run.

Testing in
TestOps/sec
My
function reversePrint (linkedList, previousValues) {
    var values = previousValues ? previousValues : [];

    values.unshift(linkedList.value)
  
    if (linkedList.next) {
        reversePrint(linkedList.next, values);
    } else {
        console.log(values.join(','));
    }
}

reversePrint({
    value: 1,
    next: {
        value: 2,
        next: {
            value: 3,
            next: {
                value: 4,
                next: null
            }
        }
    }
});
ready
Serg
var reversePrint = function(currentItem, array) {

    if(!array) {
        array = [];
    }

    while(currentItem) {
        array.unshift(currentItem.value);
        currentItem = currentItem.next
    }

    return array.join(',');
};


reversePrint({
    value: 1,
    next: {
        value: 2,
        next: {
            value: 3,
            next: {
                value: 4,
                next: null
            }
        }
    }
});
ready

Revisions

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