Test name | Executions per second |
---|---|
Sort the sorted | 3066.3 Ops/sec |
Sort the shuffled | 2980.2 Ops/sec |
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var sorted = [];
var shuffled = [];
for (var i = 0; i < 1000; i++) {
sorted.push('a' + i);
shuffled.push('a' + i);
}
shuffled = shuffle(shuffled);
var s2 = sorted.sort();
var s2 = shuffled.sort();