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
If move1 is faster for larger moves, and move2 is faster for small moves, what is the crossover point?
<script>
function newArray(size) {
var array = new Array(size);
for (i=0; i < array.length; i++) array[i] = i;
return array;
}
var arraySmall = newArray(200);
var arrayMed = newArray(300);
var arrayLarge = newArray(400);
Array.prototype.move1 = function(from, to) {
this.splice(to, 0, this.splice(from, 1)[0]);
};
Array.prototype.move2 = function(pos1, pos2) {
// local variables
var i, tmp;
// cast input parameters to integers
pos1 = parseInt(pos1, 10);
pos2 = parseInt(pos2, 10);
// if positions are different and inside array
if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this.length && 0 <= pos2 && pos2 <= this.length) {
// save element from position 1
tmp = this[pos1];
// move element down and shift other elements up
if (pos1 < pos2) {
for (i = pos1; i < pos2; i++) {
this[i] = this[i + 1];
}
}
// move element up and shift other elements down
else {
for (i = pos1; i > pos2; i--) {
this[i] = this[i - 1];
}
}
// put element from position 1 to destination
this[pos2] = tmp;
}
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
move1 - small size, small move |
| ready |
move2 - small size, small move |
| ready |
move1 - small size, big move |
| ready |
move2 - small size, big move |
| ready |
move1 - medium size, small move |
| ready |
move2 - medium size, small move |
| ready |
move1 - medium size, big move |
| ready |
move2 - medium size, big move |
| ready |
move1 - large size, small move |
| ready |
move2 - large size, small move |
| ready |
move1 - large size, big move |
| ready |
move2 - large size, big move |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.