Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test Sorting Locations etc etc etc etc
(version: 0)
Comparing performance of:
Itself vs sorted vs stableSort
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function sortString(a, b) { const upperA = a.toUpperCase(); const upperB = b.toUpperCase(); if (upperA < upperB) { return -1; } else if (upperA > upperB) { return 1; } else { return 0; } } function stableSort(values, compareFunc) { const valuesAndIndices = values.map((value, index) => [value, index]); const sortedValuesAndIndices = valuesAndIndices.sort( ([value1, index1], [value2, index2]) => { const comparison = compareFunc(value1, value2); if (comparison !== 0) { return comparison; } else { return index1 - index2; } } ); sortedValuesAndIndices.forEach(([value, origIndex], index) => { values[index] = value; }); return values; } function stableNumberSort(items, fieldName) { return stableSort( items, (item1, item2) => item1[fieldName] - item2[fieldName] ); } const locations = [{ "value": "AF", "label": "Afghanistan", "unit": "country" }, { "value": "AR", "label": "Argentina", "unit": "country" }, { "value": "522", "label": "Columbus, GA", "unit": "dma" }, { "value": "KY", "label": "Cayman Islands", "unit": "country" }, { "value": "AO", "label": "Angola", "unit": "country" }, { "value": "AG-03", "label": "Antigua and Barbuda, Parish of Saint George", "unit": "region" }, { "value": "AE-AZ", "label": "United Arab Emirates, Abu Dhabi", "unit": "region" }, { "value": "821", "label": "Bend, OR", "unit": "dma" }, { "value": "AL-02", "label": "Albania, Qarku i Durresit", "unit": "region" }, { "value": "757", "label": "Boise, ID", "unit": "dma" } ] function itself () { return locations } function sortWithLodash() { const testSort = locations.reduce((acc, next) => { acc[next.unit].push(next) return acc }, { country: [], region: [], dma: [] }) const sortedCountry = _.sortBy(testSort.country, 'label') const sortedRegion = _.sortBy(testSort.region, 'label') const sortedDma = _.sortBy(testSort.dma, 'label') return sortedCountry.concat(sortedRegion).concat(sortedDma); } function anotherSort() { const collection = { country: [], region: [], dma: [] } locations.forEach(location => { collection[location.unit].push(location) }) const a = stableSort(collection.country, (item1, item2) => sortString(item1.label, item2.label) ); const b = stableSort(collection.region, (item1, item2) => sortString(item1.label, item2.label) ); const c = stableSort(collection.dma, (item1, item2) => sortString(item1.label, item2.label) ); return [...a, ...b, ...c] }
Tests:
Itself
itself();
sorted
sortWithLodash();
stableSort
anotherSort();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Itself
sorted
stableSort
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 definition and test cases for you. **Benchmark Definition** The benchmark definition is in JSON format, which contains three main sections: 1. **Script Preparation Code**: This section includes JavaScript functions that are used to prepare the data for testing. The two notable functions are: * `sortString(a, b)`: A simple comparison function that returns -1 if `a` is less than `b`, 1 if `a` is greater than `b`, and 0 if they are equal (after converting both strings to uppercase). * `stableSort(values, compareFunc)`: A stable sorting algorithm that uses a compare function (`compareFunc`) to sort the values. The algorithm first creates an array of values along with their indices, sorts this array based on the comparison function, and then updates the original values in the order they were inserted. 2. **Benchmark Definition**: This section defines three different test cases: * `itself()`: Calls the `itself` function, which returns the `locations` array. * `sortWithLodash()`: Uses Lodash's `sortBy` function to sort the locations by their unit (country, region, or dma). * `anotherSort()`: Creates a collection of locations grouped by unit and then uses the `stableSort` function to sort each group. 3. **Html Preparation Code**: This section includes a script tag that loads Lodash version 4.17.5. **Individual Test Cases** Each test case has a unique name and corresponds to one of the three benchmark definitions: * `Itself`: Measures the performance of calling the `itself` function, which returns the `locations` array. * `sorted`: Measures the performance of sorting locations using Lodash's `sortBy` function. * `stableSort`: Measures the performance of creating a collection of grouped locations and then using the `stableSort` function to sort each group. **Latest Benchmark Results** The latest benchmark results show three test cases with their respective execution counts per second: * `Itself`: 26871316 executions/second * `sorted`: 572462.375 executions/second * `stableSort`: 491751.40625 executions/second These results indicate that the `itself` function is the fastest, followed by `sorted`, and then `stableSort`. In summary, this benchmark compares the performance of three different approaches to sorting locations: 1. Calling a simple comparison function (`sortString`) on each location individually. 2. Using Lodash's `sortBy` function to sort all locations at once. 3. Creating a collection of grouped locations and using the `stableSort` function to sort each group. The benchmark results suggest that using a single sorting algorithm (in this case, Lodash's `sortBy`) is faster than calling separate comparison functions on each location individually or creating a collection and then sorting it. However, the performance difference between these approaches may vary depending on the specific use case and data size.
Related benchmarks:
_.sortBy vs array.prototype.sort
Sorted vs unsorted Array sort
Collator vs LocaleCompare test with sort
math.min/max vs sort
Comments
Confirm delete:
Do you really want to delete benchmark?