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);
// ---------------------------------
numberArrayToSort.sort((a, b) => sortByNumberComparator(a, b, sortDirections.ASC));
numberArrayToSort.sort(sortDirectionWrapper(primitiveTypeComparator, sortDirections.ASC));
numberArrayToSort.sort(sortDirectionWrapper(compareStraight, sortDirections.ASC));
numberArrayToSort.sort(cleanStraightSort(sortDirections.ASC))
numberArrayToSort.sort(cleanStraightSortFast(sortDirections.ASC))
--enable-precise-memory-info
flag.
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? |
Test name | Executions per second |
---|---|
PR Code | 33918.4 Ops/sec |
Comparison Code | 90685.2 Ops/sec |
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:
sortByNumberComparator
function, which is a curried version of the comparison function.compare
function with the sorting direction set to ascending (sortDirections.ASC
).compareStraight
) instead of currying.cleanStraightSort
), which is similar to sortByNumberComparator
.Pros and Cons
compareStraight
can reduce overhead, but may not be as efficient as the curried version or other approaches.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:
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:
Additionally, the benchmark could also explore different sorting techniques, such as:
or even non-comparative sorting algorithms like: