jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
<style>
.debug-block {
border: 1px solid black;
margin-bottom: 15px;
}
.debug-block span:first-child {
font-weight: bold;
display: block;
}
.failed {background: salmon;}
.passed {background: lightblue;}
</style>
<script src='http://code.jquery.com/jquery.js'></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore.js">
</script>
<script>var underscore = _.noConflict();</script>
<script src="//rawgithub.com/lodash/lodash/master/dist/lodash.js"></script>
<script>var lodash = _.noConflict();</script>
<script>
var sortbyrandom = function(arr){
arr = arr.slice();
return arr.sort(function() { return 0.5 - Math.random(); });
}
var knut3 = function(arr) {
arr = arr.slice();
for (var l = arr.length; l > 1;) {
var i = Math.floor(Math.random() * l--);
var t = arr[l];
arr[l] = arr[i];
arr[i] = t;
}
return arr;
}
</script>
<i>Example output from methods. (lightblue - passed; salmon - failed)</i>
<div id="debug"></div>
<script>
var $debug = $('#debug');
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'foo', 'bar', true, false];
function test(arr, func) {
arr = arr.slice();
var res = func(arr), testOk = true;
if (!lodash.isEmpty(lodash.difference(arr, res))) {
console.log('diff', lodash.difference(arr, res));
testOk = false;
}
if (arr.join(',') == res.join(',')) {
console.log('not shuffled', arr.join(','), res.join(','));
testOk = false;
}
if (arr.sort().join(',') != res.sort().join(',')) {
console.log('not same', arr.sort().join(','), res.sort().join(','));
testOk = false;
}
return testOk;
}
function debugWrite(testname, arr, func) {
var $div = $('<div></div>', {class:'debug-block'});
$div.addClass(test(arr, func) ? 'passed' : 'failed');
var html = '<span>'+testname+'</span>';
html += '<span>'+JSON.stringify(func(arr))+'</span>';
$div.html(html);
$debug.append($div);
}
/******************************************/
debugWrite('underscore_shuffle', arr, function (o) { return underscore.shuffle(o); });
debugWrite('lodash_shuffle', arr, function (o) { return lodash.shuffle(o); });
debugWrite('sortbyrandom', arr, function (o) { return sortbyrandom(o); });
debugWrite('knut3', arr, function (o) { return knut3(o); });
/******************************************/
var arr = Array(100);
for (var i = 0; i < 100; i++) {
arr[i] = i;
}
</script>
var lodash = window.lodash,
knut3 = window.knut3,
sortbyrandom = window.sortbyrandom,
underscore = window.underscore;
Ready to run.
Test | Ops/sec | |
---|---|---|
Underscrore.shuffle |
| ready |
native sort by random |
| ready |
Knuth-Fisher-Yates |
| ready |
lodash.shuffle |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.