Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Heap sort vs Native sort
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser:
Firefox 124
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Heap
1003376.2 Ops/sec
Native sort
7032139.5 Ops/sec
Script Preparation code:
var arr = [40, 10, 50, 24, 1, 2, 4, -10, 15, 7, 8, 5];
Tests:
Heap
const { floor } = Math; function heapsort(arr, comparator = ascendantComparator) { const count = arr.length; let end = count - 1; heapify(arr, comparator); while (end > 0) { [arr[end], arr[0]] = [arr[0], arr[end]]; end = end - 1; siftDown(arr, 0, end, comparator); } return arr; } function ascendantComparator(a, b) { return a > b ? 1 : a < b ? -1 : 0; } function heapify(array, comparator) { const count = array.length; let start = floor((count - 2) / 2); while (start >= 0) { siftDown(array, start, count - 1, comparator); start = start - 1; } } function siftDown(arr, start, end, comparator) { let root = start; while (root * 2 + 1 <= end) { const lChild = root * 2 + 1; const rChild = lChild + 1; let swap = root; if (comparator(arr[swap], arr[lChild]) < 0) swap = lChild; if (rChild <= end && comparator(arr[swap], arr[rChild]) < 0) swap = rChild; if (swap === root) return; [arr[root], arr[swap]] = [arr[swap], arr[root]]; root = swap; } } heapsort(arr);
Native sort
function sortNumber(a, b) { return a - b; } arr.sort(sortNumber);