var sampleData = [];
for (var i=0; i < 100000; i++){
sampleData.push({value: (Math.floor(Math.random() * 10000))});
}
var selectorFn = function(x) {
return x.value;
}
var combSort = function (source, selectorFn, sortDirection) {
var result = source.slice();
var count = result.length;
var gap = count;
var swapped = true;
while (gap > 1 || swapped) {
if (gap > 1) {
gap = Math.floor(gap / 1.24733);
}
var i = 0;
swapped = false;
while ((i + gap) < count) {
var item1 = result[i];
var item2 = result[i + gap];
if (shouldBeSwaped(selectorFn(item1), selectorFn(item2), selectorFn, sortDirection)) {
result[i] = item2;
result[i + gap] = item1;
swapped = true;
}
i++;
}
}
return result;
}
var shouldBeSwaped = function(value, valueToCompare, selectorFn, sortDirection) {
var less = value > valueToCompare;
return sortDirection === "asc" ? less : !less;
}
var order = "desc";
sampleData.sort(function(a,b){
var x = selectorFn(a); var y = selectorFn(b);
var res = ((x < y) ? -1 : ((x > y) ? 1 : 0));
return order === "desc" ? -1*res : res;
})
var order = "desc";
sampleData.sort(function(a,b){
var x = selectorFn(a); var y = selectorFn(b);
var res = ((x < y) ? -1 : ((x > y) ? 1 : 0));
return order === "desc" ? -1*res : res;
})
var order = "asc";
combSort(sampleData, selectorFn, order);
var order = "asc";
combSort(sampleData, selectorFn, order);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native Sort DESC | |
Native Sort ASC | |
Comb Sort DESC | |
Comb Sort ASC |
Test name | Executions per second |
---|---|
Native Sort DESC | 3.5 Ops/sec |
Native Sort ASC | 3.4 Ops/sec |
Comb Sort DESC | 2.9 Ops/sec |
Comb Sort ASC | 2.9 Ops/sec |
Benchmark Explanation
The provided benchmark compares the performance of two sorting algorithms: Native JavaScript sort and Comb Sort. The benchmark uses a sample dataset of 100,000 random integers and measures the execution time for each sorting algorithm in descending (ASC) and ascending (DESC) order.
Native JavaScript Sort
Native JavaScript sort is implemented using the sort()
method with a custom comparison function that compares two elements based on their values. The comparison function returns an integer value indicating the direction of the sort:
-1
if the first element is less than the second1
if the first element is greater than the second0
if the elements are equalThe native JavaScript sort implementation uses a variation of the Timsort algorithm, which is a hybrid sorting algorithm derived from merge sort and insertion sort.
Comb Sort
Comb Sort is an optimized version of Bubble Sort that uses a combination of two techniques:
Pros and Cons
Native JavaScript Sort:
Comb Sort:
Library: Timsort
Timsort is a hybrid sorting algorithm that combines elements of merge sort and insertion sort. It is used as the underlying implementation for the native JavaScript sort()
method.
Special JS Feature/ Syntax
This benchmark does not use any special JavaScript features or syntax, such as async/await, Promises, or modern ES6+ features.
Alternatives
Other sorting algorithms that could be compared to Native JavaScript Sort and Comb Sort include:
These alternatives may offer better performance or efficiency for specific use cases, but the trade-offs in terms of implementation complexity, cache friendliness, and stability must be carefully considered.