Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sortBy vs orderBy1
(version: 0)
Lodash sortBy vs orderBy
Comparing performance of:
_.sortBy vs _.orderBy
Created:
6 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 getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value: ''+getRandomInt(100)}); }
Tests:
_.sortBy
_.sortBy(arr, function({value}) { return value.toLowerCase() })
_.orderBy
_.orderBy(arr, function({value}) { return value.toLowerCase() })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sortBy
_.orderBy
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):
Let's break down the provided benchmarking scenario. **Overview** The benchmark compares two functions from the Lodash library: `_.sortBy` and `_.orderBy`. Both functions are used to sort an array of objects based on a specific key (`value`) in each object. The test case is designed to measure the performance difference between these two sorting approaches. **Lodash Library and its Purpose** Lodash is a JavaScript utility library that provides a collection of functional programming helpers. In this case, `_.sortBy` and `_.orderBy` are used to sort arrays of objects based on a specified key. * `_.sortBy(arr, function({value}) { return value.toLowerCase() })`: This function sorts the array `arr` in ascending order based on the `value` property of each object. The sorting is case-insensitive. * `_.orderBy(arr, function({value}) { return value.toLowerCase() })`: This function also sorts the array `arr` in ascending order based on the `value` property of each object. However, unlike `_.sortBy`, it performs a stable sort (i.e., the order of equal elements is preserved). **Options Compared** The two options being compared are: 1. `_.sortBy(arr, function({value}) { return value.toLowerCase() })`: This approach sorts the array in ascending order using a case-insensitive comparison. 2. `_.orderBy(arr, function({value}) { return value.toLowerCase() })`: This approach also sorts the array in ascending order using a case-insensitive comparison, but it is more stable than `_.sortBy` since it preserves the original order of equal elements. **Pros and Cons** * **_.sortBy**: Pros: + Fast execution (since it uses a simple comparison function). + Inefficient for large datasets with many duplicate values (leading to poor cache locality). * **_.orderBy**: Pros: + More stable than `_.sortBy` since it preserves the original order of equal elements. + Better cache locality, leading to faster execution times. Cons: * Slower execution due to the use of a sorting algorithm (like Timsort or Merge Sort). **Device Platform and Browser Considerations** The benchmark uses Chrome Mobile 76 on an Android device. As mobile devices often have limited resources compared to desktop environments, the performance differences between `_.sortBy` and `_.orderBy` might be more pronounced in these scenarios. **Other Alternatives** * For sorting arrays without preserving order (i.e., when all elements are unique), you could use `_.sortBy(arr, function({value}) { return value.toLowerCase() })`. * If stability is not a concern, you can use `arr.sort((a, b) => a.value.localeCompare(b.value))` instead of Lodash's sorting functions. * For more complex sorting scenarios, consider using a dedicated sorting library like Quicksort or Merge Sort. Keep in mind that the best approach depends on your specific requirements and performance constraints.
Related benchmarks:
Lodash orderBy vs array.prototype.sort
Lodash orderBy() vs array.prototype.sort
Lodash sortBy vs orderBy performance
Lodash orderBy vs array.prototype.sort fork
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?