Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sorting speed comparison
(version: 10)
Test of sorting speed and approach to currying
Comparing performance of:
PR Code vs Comparison Code vs Comparison Code 2, simple sort function vs Comparison Code 3, syntactic sugar vs Comparison Code 4, now even quicker?
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var sortDirections = { DEFAULT: -1, ASC: 1, DESC: -1, }; var numberArrayToSort = [1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7, 1, 4, 3, 2, 5, 6, 9, 10, 5, 8, 7]; // --------------------------------- var compare = (lessThan, equalTo) => { if (equalTo) { return 0; } if (lessThan) { return -1; } return 1; }; var compareStraight = (a, b) => { if (a === b) { return 0; } if (a < b) { return -1; } return 1; }; var compareStraightNumber = (a, b) => a - b; // --------------------------------- var primitiveTypeComparator = (a, b) => compare(a < b, a === b); var genericDirectionComparator = (comparator = primitiveTypeComparator) => (a, b, direction = sortDirections.ASC) => { return direction * comparator(a, b); }; var sortByNumberComparator = genericDirectionComparator(); // --------------------------------- var sortDirectionWrapper = (compareFunc, direction = sortDirections.ASC) => (a, b) => compareFunc(a, b) * direction; var cleanStraightSort = (direction) => sortDirectionWrapper(compareStraight, direction); var cleanStraightSortFast = (direction) => sortDirectionWrapper(compareStraightNumber, direction); // ---------------------------------
Tests:
PR Code
numberArrayToSort.sort((a, b) => sortByNumberComparator(a, b, sortDirections.ASC));
Comparison Code
numberArrayToSort.sort(sortDirectionWrapper(primitiveTypeComparator, sortDirections.ASC));
Comparison Code 2, simple sort function
numberArrayToSort.sort(sortDirectionWrapper(compareStraight, sortDirections.ASC));
Comparison Code 3, syntactic sugar
numberArrayToSort.sort(cleanStraightSort(sortDirections.ASC))
Comparison Code 4, now even quicker?
numberArrayToSort.sort(cleanStraightSortFast(sortDirections.ASC))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
PR Code
Comparison Code
Comparison Code 2, simple sort function
Comparison Code 3, syntactic sugar
Comparison Code 4, now even quicker?
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares different approaches to sorting an array of numbers in JavaScript. The sorting algorithm used is not explicitly specified, but it's assumed to be a stable sort like Merge Sort or Insertion Sort, which maintains the relative order of equal elements. **Tested Options** There are four test cases: 1. **PR Code**: Uses the `sortByNumberComparator` function, which is a curried version of the comparison function. 2. **Comparison Code**: Directly calls the `compare` function with the sorting direction set to ascending (`sortDirections.ASC`). 3. **Comparison Code 2 (simple sort)**: Similar to Comparison Code, but uses a simpler sorting function (`compareStraight`) instead of currying. 4. **Comparison Code 3 (syntactic sugar)**: Uses a syntactic sugar version of the comparison function (`cleanStraightSort`), which is similar to `sortByNumberComparator`. 5. **Comparison Code 4 (now even quicker?)**: A variant of Comparison Code 3, optimized for performance by removing unnecessary checks. **Pros and Cons** * **Currying (PR Code vs Comparison Code)**: Currying can improve code readability and make it easier to compose functions, but it may also incur a small performance overhead due to the additional function call. * **Simplification (Comparison Code 2 vs Comparison Code)**: Using a simpler sorting function like `compareStraight` can reduce overhead, but may not be as efficient as the curried version or other approaches. * **Syntactic sugar (Comparison Code 3 and 4)**: Syntactic sugar can make code easier to read and write, but may also add some overhead due to the compiler or interpreter. **Library/Function Explanation** The `compare` function is a simple comparison function that returns -1 if the first element is less than the second, 0 if they are equal, and 1 if the first element is greater. The `sortByNumberComparator` function is a curried version of this comparison function, which allows it to be composed with other functions. **Special JS Feature/Syntax** The benchmark uses some advanced JavaScript features, including: * Currying (function composition) * Syntactic sugar (using `cleanStraightSort` instead of the full `sortDirectionWrapper` function) However, these features are not specific to JavaScript and can be found in other programming languages as well. **Other Alternatives** Other sorting algorithms that could be tested in this benchmark include: * Quick Sort * Heap Sort * Radix Sort (for large datasets) * Timsort (a hybrid of Merge Sort and Insertion Sort) Additionally, the benchmark could also explore different sorting techniques, such as: * Bubble Sort * Selection Sort * Insertion Sort or even non-comparative sorting algorithms like: * Counting Sort * Bucket Sort
Related benchmarks:
Array Sorting Methods
Array.sort vs Math.min+Math.max (LONG ARRAYS)
Sort method comparisons (quicksort, for loop, Arra.prototype.sort)
sorting an array and then reverse vs sorting with reversive comparison
Comments
Confirm delete:
Do you really want to delete benchmark?