Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comb Sort vs. Native Sort
(version: 0)
Comparing performance of:
Native Sort DESC vs Native Sort ASC vs Comb Sort DESC vs Comb Sort ASC
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var sampleData = []; for (var i=0; i < 1000; 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) { var less = value > valueToCompare; return sortDirection === "asc" ? less : !less; }
Tests:
Native Sort DESC
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; })
Native Sort ASC
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; })
Comb Sort DESC
var order = "asc"; combSort(sampleData, selectorFn, order);
Comb Sort ASC
var order = "asc"; combSort(sampleData, selectorFn, order);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Native Sort DESC
Native Sort ASC
Comb Sort DESC
Comb Sort ASC
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 JSON benchmark definitions and explain what's being tested, compared options, pros and cons, library usage, special JavaScript features, and other considerations. **Benchmark Definition** The benchmark is comparing two sorting algorithms: Native Sort (also known as Timsort in modern browsers) and Comb Sort. The goal is to determine which algorithm performs better for a specific dataset size. **Script Preparation Code** The script generates an array of 1000 random objects with a `value` property between 0 and 999. This will be used as the input data for both sorting algorithms. **Benchmark Options** There are four benchmark options: 1. **Native Sort DESC**: Tests Native Sort in descending order. 2. **Native Sort ASC**: Tests Native Sort in ascending order. 3. **Comb Sort DESC**: Tests Comb Sort in descending order. 4. **Comb Sort ASC**: Tests Comb Sort in ascending order. **Library Usage** The `selectorFn` function is used to compare two objects in the array. It's defined as a simple getter function that returns the value property of an object. Both sorting algorithms use this function to compare elements. **Special JavaScript Features** None are explicitly mentioned, but note that modern browsers' Timsort implementation uses some advanced techniques like: * Using the `Array.prototype.sort()` method internally, which involves comparing elements in a more efficient way than a simple callback function. * Leveraging the cache to reduce comparisons during iteration. **Pros and Cons of Options** 1. **Native Sort**: Pros: * Optimized for large datasets and stable sorting (less swaps). * Uses the `Array.prototype.sort()` method, which is optimized by modern browsers. Cons: * Not optimized for small datasets or unsorted data. 2. **Comb Sort**: Pros: * Well-suited for small to medium-sized datasets with some order (reduces comparisons). Cons: * Has higher overhead due to the gap reduction mechanism and more swaps compared to Native Sort. **Other Considerations** * The dataset size of 1000 elements is likely chosen for its moderate complexity, allowing both algorithms to exhibit their performance characteristics. * The use of a random `selectorFn` ensures that the comparison logic remains consistent across different sorting implementations. **Alternatives** Other sorting algorithms could be used as alternatives in benchmarking, such as: 1. **Quicksort**: A popular and efficient algorithm for large datasets, but may perform worse on smaller datasets. 2. **Merge Sort**: Another efficient algorithm suitable for small to medium-sized datasets. 3. **Heapsort**: Suitable for certain use cases where stability is important, like sorting a priority queue. Keep in mind that the choice of alternative algorithms depends on the specific requirements and characteristics of the dataset being sorted.
Related benchmarks:
Comb Sort vs. Native Sort
Comb Sort vs. Native Sort
Comb Sort vs. Native Sort
Javascript Sorting Algorithms (Best case, almost ordered)
Comments
Confirm delete:
Do you really want to delete benchmark?