Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sortBy vs orderBy refactored
(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):
Let's break down the provided benchmark and explain what's being tested, compared, and other considerations. **Benchmark 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 property (`"value"`). **What's Being Tested?** The test measures the performance difference between using `sortBy` and `orderBy` functions. The benchmark creates a large array of 100,000 objects with random "value" properties and then passes this array to both functions. **Comparison Options** There are two main options being compared: 1. **_.sortBy(arr, "value")**: This option uses the `_sortBy` function from Lodash to sort the array based on the "value" property. 2. **_.orderBy(arr, "value")**: This option uses the `_orderBy` function from Lodash to sort the array based on the "value" property in descending order. **Pros and Cons of Each Approach** 1. **_.sortBy(arr, "value")** * Pros: More flexible, can be used for ascending or descending orders by passing a second argument (e.g., `_.sortBy(arr, "value", "desc")`). * Cons: May be slower than `_orderBy` due to the need to calculate the order manually. 2. **_.orderBy(arr, "value")** * Pros: Faster, as it uses an optimized sorting algorithm that takes into account the property being sorted on. * Cons: Less flexible, only sorts in descending order by default. **Lodash Library** The Lodash library is a popular utility library for JavaScript that provides a wide range of functional programming helpers. In this case, both `_sortBy` and `_orderBy` functions are used to simplify array sorting tasks. **Other Considerations** * The benchmark uses a large array of 100,000 objects, which may not be representative of smaller datasets. * The `getRandomInt` function is used to generate random values for the "value" property, which can affect the performance results. * The benchmark runs on a Chrome 78 browser on a Mac OS X 10.15.1 desktop platform. **Alternative Approaches** If you need more control over the sorting process or want to avoid using Lodash, you could consider: 1. Using the `Array.prototype.sort()` method directly on the array. 2. Implementing your own custom sorting algorithm (e.g., merge sort or quicksort). 3. Using a different library that provides similar functionality, such as Ramda. Keep in mind that these alternatives may require more code and expertise, but can provide more flexibility and control over the sorting process.
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?