var sampleData = [];
for (var i=0; i < 100; i++){
sampleData.push({value: (Math.floor(Math.random() * 1000))});
}
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) {
let 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 = "asc";
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 | 4445.7 Ops/sec |
Native Sort ASC | 3932.7 Ops/sec |
Comb Sort DESC | 5221.1 Ops/sec |
Comb Sort ASC | 5122.5 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark measures the performance of two sorting algorithms: Native Sort and Comb Sort, on an array of 100 random integers. The benchmark is run in descending order (DESC) and ascending order (ASC).
Native Sort
Native Sort uses the built-in sort()
method in JavaScript, which sorts the elements of an array in place and returns the sorted array.
The benchmark definition for Native Sort contains a lambda function that compares two elements using the selectorFn
function, which extracts the value from each object. The comparison is done in ascending order if the "order" variable is set to "asc", and in descending order if it's set to "desc".
Comb Sort
Comb Sort is a hybrid sorting algorithm derived from both Insertion Sort and Selection Sort. It starts with a gap of 1 and then gradually decreases the gap by a fixed amount (in this case, 1.24733) until the gap reaches 1.
The benchmark definition for Comb Sort contains a call to the combSort
function, which takes three arguments: the array to sort (sampleData
), the selector function (selectorFn
), and the sorting direction ("asc" or "desc").
Library
The selectorFn
function is used as a library to extract the value from each object in the array. This function is not part of the standard JavaScript library, but rather a custom implementation that returns the value associated with each element.
Special JS Feature/Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark.
Pros and Cons
Here are some pros and cons of using Native Sort versus Comb Sort:
Native Sort: Pros:
Cons:
Comb Sort: Pros:
Cons:
combSort
function)Other Alternatives
Some other sorting algorithms that could have been used in this benchmark include:
These algorithms might have been more suitable for certain types of data or performance requirements. However, Native Sort is generally considered to be one of the fastest and most efficient sorting algorithms in JavaScript.