Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sortBy vs orderBy (fixed)
(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,"value");
_.orderBy
_.orderBy(arr,"value");
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):
I'd be happy to explain the benchmark and its options. **Benchmark Overview** The benchmark measures the performance of two Lodash functions: `sortBy` and `orderBy`. Both functions are used to sort an array of objects based on a specific property ("value" in this case). **Options Compared** The two options being compared are: 1. `_.sortBy(arr, "value")` 2. `_.orderBy(arr, "value")` Both functions take the same input (an array `arr` and a string key `"value"`). However, they differ in their implementation. **Pros and Cons of Each Approach** ### _.sortBy * **Pros:** + More flexible, as it allows sorting in descending order by default + Can handle arrays with duplicate keys * **Cons:** + May be slower due to the need to check for duplicate keys and adjust the sort order accordingly + Requires more processing power to handle edge cases ### _.orderBy * **Pros:** + Faster, as it uses a simpler implementation that doesn't require checking for duplicate keys + More efficient for large datasets * **Cons:** + Only works in ascending order by default (no option to sort in descending order) + May not handle arrays with duplicate keys correctly **Lodash Library and Its Purpose** The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object creation, and more. In this benchmark, Lodash is used to provide the `sortBy` and `orderBy` functions. The `_.sortBy` function sorts an array of objects based on a specific property (in this case, "value"). The `_._orderBy` function does essentially the same thing, but with some differences in implementation (as described above). **Special JavaScript Feature or Syntax** None are mentioned in the benchmark code. However, it's worth noting that Lodash uses a few advanced JavaScript features, such as: * Closures (in the `getRandomInt` function) * Functions (e.g., `_.sortBy`, `_._orderBy`) * Object property access (e.g., `arr.push({value:getRandomInt(100)})`) These features are not unusual in modern JavaScript code, but may require some familiarity with the language to understand. **Alternatives** If you're interested in alternative sorting libraries or implementations, here are a few options: 1. **Vanilla JavaScript:** You can implement your own sorting functions using basic JavaScript operations (e.g., `Array.prototype.sort()`). 2. **Other Lodash functions:** Lodash has many other useful functions for array manipulation and object creation. Some examples include `_filter()`, `_map()`, and `_reduce()`. 3. **External libraries:** There are many external libraries available that provide sorting functionality, such as the `lodash-sort` library or the `fast-sort` library. 4. **Custom implementations:** You can also implement your own sorting algorithms using standard computer science techniques (e.g., quicksort, mergesort).
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
lodash sortBy vs native sortBy 4.17.21
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?