Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Sorting test
(version: 0)
Benchmark.js
Comparing performance of:
custom sort vs normal sort
Created:
7 years ago
by:
Guest
Go to the latest result
Tests:
custom sort
const array1 = [1, 3, 9]; const array2 = [2, 9, 4]; let array = mergeSort(array1, array2); console.log(array); function mergeSort(array1, array2) { //comparing two popular merging techniques spread is slightly faster so that's being used //merge arrays let mergedArray = [...array1, ...array2]; //sort arrays // loop through array for (let i = 0; i < mergedArray.length - 1; i++) { //set first index let min = i; //loop through array again for (let j = i + 1; j < mergedArray.length; j++) { //check if number in first array is smaller if reset index if (mergedArray[j] < mergedArray[min]) { min = j; } } //if index of first for has been reset above it's diffirent which means we can add it to end of the array if (min != i) { let target = mergedArray[i]; mergedArray[i] = mergedArray[min]; mergedArray[min] = target; } } return mergedArray; //return mergedArray.sort((a,b) => a-b); }
normal sort
const array1 = [1, 3, 9]; const array2 = [2, 9, 4]; let array = mergeSort(array1, array2); console.log(array); function mergeSort(array1, array2) { //comparing two popular merging techniques spread is slightly faster so that's being used //merge arrays let mergedArray = [...array1, ...array2]; //sort arrays return mergedArray.sort((a,b) => a-b); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
custom sort
normal sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
custom sort
333K/s
normal sort
327K/s
View exact numbers
Test name
Executions per second
✓
custom sort
333,163 Ops/sec
normal sort
326,950 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark measures the performance of two sorting algorithms: mergeSort (also known as merge and sort) and regular sort. The test cases are identical, except for how they approach merging and sorting the arrays. **Options Compared** Two options are compared: 1. **Merge and Sort**: This method merges the two input arrays using the spread operator (`[...array1, ...array2]`) and then sorts the merged array using a custom comparison function (`(a,b) => a-b`). 2. **Regular Sort**: This method uses the built-in `sort()` method of JavaScript arrays, which internally implements a sorting algorithm called Timsort. **Pros and Cons** ### Merge and Sort Pros: * The spread operator is slightly faster than using an array concatenation (`array1.concat(array2)`). * It avoids the overhead of calling a separate sort function. * It allows for more control over the merging process. Cons: * Requires custom implementation of the sorting logic. * Can be less efficient if not optimized correctly. * May not work as expected if the input arrays have different lengths or data types. ### Regular Sort Pros: * Built-in and well-tested, so you can trust its performance. * Handles edge cases (e.g., null or undefined values) more robustly. * Often faster than custom implementations for large datasets. Cons: * May be slower than a custom implementation optimized for your specific use case. * Less control over the sorting process. **Other Considerations** When choosing between these two options, consider the following factors: * Performance: If speed is critical and you're dealing with very large datasets or performance-critical code, a custom implementation might be faster. * Complexity: If you want to avoid implementing a sorting algorithm from scratch, regular sort is often the simpler choice. **Library Used** In this benchmark, no external libraries are used. The `sort()` method is part of the JavaScript standard library. **Special JS Feature/Syntax** The use of template literals (`\r\nconst array = mergeSort(array1, array2);\r\n\r\nconsole.log(array);`) and destructuring assignment (`let [a,b] = mergedArray[0].split(')')`) are not used extensively in this benchmark. However, it's worth noting that the `mergeSort` function uses a technique called "in-place sorting," which modifies the original array instead of creating a new one. This can be an efficient approach for large datasets but may lead to unexpected behavior if not handled correctly. **Alternatives** If you're interested in exploring other sorting algorithms or optimizations, consider the following alternatives: * **Quicksort**: A popular in-place sorting algorithm with an average time complexity of O(n log n). * **Heapsort**: Another efficient in-place sorting algorithm with a time complexity of O(n log n) in the worst case. * **Radix sort**: A non-comparative sorting algorithm suitable for large datasets of integers or strings. These alternatives can provide different trade-offs between performance, complexity, and memory usage. You may need to experiment and profile your specific use case to determine the most suitable algorithm.
Related benchmarks
Intl.Collator vs Sort
sorting an array and then reverse vs sorting with reversive comparison
Comments
Confirm delete:
Do you really want to delete benchmark?