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
Based on version 8 of this test. No improvements over the initial code except for the 2 variable loop alternatives that show some difference on how browsers optimise them.
Comments from the original post (case #8):
Checking performance of Array.reverse() vs custom loops.
Also see http://jsperf.com/forloop-vs-reverse-while-loop-and-array-reverse/9.
Edited to make sure JIT isn't being "smart" and doing some extra optimizations that won't exist on real applications, also increased the number of items to 1000 to represent a common scenario (perf usually changes drastically based on amount of items, some techniques might only be fast for a short amount of items). See: http://blog.millermedeiros.com/benchmarking-is-hard/
// at least 1K items to make sure it's fast, performnace usually changes
// based on the amount of items
var array = [];
var n = 1000;
while (n--) {
array.push(n * Math.random());
}
// we store result outside of test scope to make sure JIT isn't simply ignoring
// the operations on some cases
var result;
function forSwap(array){
var length = array.length;
var left = null;
var right = null;
for (left = 0, right = length - 1; left < right; left += 1, right -= 1) {
var temporary = array[left];
array[left] = array[right];
array[right] = temporary;
}
return array;
}
function forSwapHalf(array){
var length = array.length;
var left = null;
var right = null;
for (left = 0; left < length / 2; left += 1) {
right = length - 1 - left;
var temporary = array[left];
array[left] = array[right];
array[right] = temporary;
}
return array;
}
function forSwap2(a){
for (var i=0, j=a.length - 1, t; i < j; i++, j--) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
return array;
}
function whileSwap(a){
var i = 0, j = a.length - 1, t
while (i < j) {
t = a[i];
a[i++] = a[j];
a[j--] = t;
}
return a;
}
function forSwapHalf2(array){
var length = array.length;
for (var i=0, j=length-1, t; i < length / 2; i++, j--) {
t = array[i];
array[i] = array[j];
array[j] = t;
}
return array;
}
function forSwapHalf3(array){
var length = array.length;
for (var i=0, j=length-1, t; i < length / 2; i++) {
t = array[i];
array[i] = array[j];
array[j--] = t;
}
return array;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Array.reverse() |
| ready |
for swap |
| ready |
for swap half |
| ready |
for swap tweek |
| ready |
while swap |
| ready |
for swap half tweek |
| ready |
for swap half tweek 2 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.