Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Heap sort vs Native sort
(version: 0)
Comparing performance of:
Heap vs Native sort
Created:
6 years ago
by:
Guest
Jump to the latest result
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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Heap
Native sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Heap
1003376.2 Ops/sec
Native sort
7032139.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark compares two sorting algorithms: Heap sort and Native sort (built-in JavaScript sort function). **Options Compared** 1. **Heap Sort**: A comparison-based sorting algorithm that uses a binary heap data structure to sort elements. 2. **Native Sort (JavaScript's built-in sort function)**: A stable, efficient sorting algorithm that uses a combination of techniques, including quicksort and heapsort, to sort elements. **Pros and Cons** * **Heap Sort**: + Pros: Simple to implement, stable, and efficient for small datasets. + Cons: Not as efficient as Native Sort for large datasets due to its O(n log n) time complexity. + Note: The implementation in the benchmark uses a custom heapify function, which may not be optimal. * **Native Sort (JavaScript's built-in sort function)**: + Pros: Optimized and highly efficient for large datasets, stable, and widely supported. + Cons: Can be slower than Heap Sort for small datasets due to its overhead. + Note: The implementation in the benchmark is a simple comparison-based approach, which may not utilize all the optimizations available in modern JavaScript engines. **Library Used** None. The benchmark uses built-in JavaScript functions (e.g., `sort()`, `Math.floor()`). **Special JS Features/Syntax** * None mentioned in the provided code. Now, let's look at some alternative approaches: 1. **Quicksort**: Another popular sorting algorithm that can be used as a comparison to Native Sort. 2. **Merge Sort**: A stable sorting algorithm that is similar to Native Sort but has better performance for certain data distributions. 3. **Radix Sort**: A non-comparison sorting algorithm that can be used for specific use cases, such as sorting integers or strings. To run this benchmark on a different environment or platform, you would need to: 1. Update the `Script Preparation Code` with the necessary modifications to run on your target environment (e.g., changing the browser or adding platform-specific code). 2. Adjust the benchmark settings, such as the dataset size or number of executions per second. 3. Re-run the benchmark using a tool like MeasureThat.net or write your own benchmarking script. Keep in mind that the performance results may vary depending on the specific environment and configuration used.
Related benchmarks:
Sorting speed comparison
arr.sort((a, b) => a - b)[0] vs. Math.min(...arr)
Array.sort vs Math.min+Math.max (LONG ARRAYS)
Custom sort vs typed array sort
Sort method comparisons (quicksort, for loop, Arra.prototype.sort)
Comments
Confirm delete:
Do you really want to delete benchmark?