// 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());
shuffleNikolay(window.array_10000);
shuffleOliver(window.array_10000);
shuffleAndrea(window.array_10000);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Nikolay | |
Oliver | |
Andrea |
Test name | Executions per second |
---|---|
Nikolay | 219.3 Ops/sec |
Oliver | 311.8 Ops/sec |
Andrea | 312.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark definition json provides a JavaScript function to shuffle an array using three different approaches:
shuffleNikolay(array)
: This function shuffles the array by first creating a copy of the original array, then repeatedly popping elements from the end of the copied array and pushing them to the beginning.shuffleOliver(array)
: This function uses a custom shuffle algorithm that iterates through the array from right to left, swapping each element with a random element from the remaining unshuffled portion of the array.shuffleAndrea(array)
: This function shuffles the array by repeatedly iterating through the array and swapping adjacent elements.Options being compared
The three shuffle algorithms are being compared to determine which one is the most efficient in terms of time complexity (i.e., how fast it executes).
Pros and Cons of each approach:
shuffleNikolay(array)
: This approach has a time complexity of O(n), where n is the length of the array, making it relatively efficient.shuffleOliver(array)
: This approach has a time complexity of O(n), where n is the length of the array, making it relatively efficient.shuffleNikolay
.shuffleAndrea(array)
: This approach has a time complexity of O(n), where n is the length of the array, making it relatively efficient.shuffleNikolay
, this approach creates an extra copy of the original array.Library usage
None of the provided functions use any external libraries. However, the Math.random()
function is used throughout the benchmark definition and individual test cases.
Special JS features or syntax
There are no special JavaScript features or syntax being used in this benchmark.
Other alternatives
If an alternative implementation were needed, other shuffle algorithms could be considered, such as:
However, it's worth noting that these alternatives may not offer significant performance improvements over the existing implementations.
In summary, this benchmark tests three different shuffle algorithms and compares their execution times to determine which one is the most efficient. The shuffleNikolay
and shuffleAndrea
approaches are relatively simple but create an extra copy of the original array, while the shuffleOliver
approach is more complex but does not create any extra copies.