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
Comparing Partial Sort implementations
// A few Heap-functions that operate on an array
function maxSiftDown(arr, i=0, comparator, value=arr[i]) {
if (i >= arr.length) return;
while (true) {
var j = i*2+1;
if (j+1 < arr.length && comparator(arr[j], arr[j+1]) < 0) j++;
if (j >= arr.length || comparator(arr[j], value) <= 0) break;
arr[i] = arr[j];
i = j;
}
arr[i] = value;
}
function maxHeapify(arr, comparator) {
for (var i = arr.length>>1; i--; ) maxSiftDown(arr, i, comparator);
return arr;
}
// The main algorithm
function partialSortWithMaxHeap(items, k, comparator) {
var heap = maxHeapify(items.slice(0, k), comparator);
for (var i = k, len = items.length; i < len; ++i) {
var item = items[i];
if (item < heap[0]) maxSiftDown(heap, 0, comparator, item);
}
return heap.sort(comparator);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Native Sort + Truncate |
| ready |
Max Heap |
| ready |
No Operation (Measuring constructing the array) |
| ready |
Sanity (Ensuring they yield the same result) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.