Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15
Safari 16
Mac OS X 10.15.7
Desktop
2 years ago
Test name Executions per second
Nikolay 93.2 Ops/sec
Oliver 432.1 Ops/sec
Andrea 442.5 Ops/sec
Script Preparation code:
x
 
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive
function getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive
}
function shuffleOliver(array) {
    let current;
    let top;
    let tmp = (current = top = array?.length);
    if (!top) {
        return array;
    }
    while (--top) {
        current = getRandomIntInclusive(0, top);
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }
    return array;
};
function shuffleAndrea(array) {
    let {length} = array;
    while (length--) {
        let i = getRandomIntInclusive(0, length);
        [array[i], array[length]] = [array[length], array[i]];
    }
    return array;
};
function shuffleNikolay(array) {
    const t = array.splice(0, array.length);
    while (t.length > 0) {
        const index = getRandomIntInclusive(0, t.length - 1);
        array.push(t.splice(index, 1)[0]);
    }
    return array;
};
window.array_10000 = Array.from(Array(10000).keys());
Tests:
  • Nikolay

     
    shuffleNikolay(window.array_10000);
  • Oliver

     
    shuffleOliver(window.array_10000);
  • Andrea

     
    shuffleAndrea(window.array_10000);