Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sortBy descending vs Lodash orderBy descending
(version: 0)
Lodash sortBy descending vs Lodash orderBy descending
Comparing performance of:
_.sortBy vs _.orderBy
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 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").reverse();
_.orderBy
_.orderBy(arr,"value", ["desc"]);
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:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sortBy
28.0 Ops/sec
_.orderBy
27.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. The provided JSON represents two benchmark tests on MeasureThat.net: `Lodash sortBy descending vs Lodash orderBy descending`. These benchmarks compare the performance of two popular utility libraries in JavaScript, Lodash, for sorting arrays in descending order. **Options being compared:** 1. **_.sortBy(arr,"value").reverse();** 2. **_.orderBy(arr,"value", ["desc"]);** Both tests use Lodash's `sortBy` and `orderBy` functions to sort the array. The difference lies in how they handle the sorting: * `_.sortBy` reverses the sorted array using the `.reverse()` method. * `_.orderBy` returns a new array with the elements sorted in descending order without reversing it. **Pros and Cons of each approach:** 1. **_.sortBy(arr,"value").reverse();** * Pros: + Easy to understand and implement. + Avoids creating an additional array, reducing memory allocation overhead. * Cons: + Reverses the sorted array, which might affect subsequent operations. 2. **_.orderBy(arr,"value", ["desc"]);** * Pros: + Returns a new sorted array without modifying the original array. + Efficiently handles large datasets. * Cons: + Creates an additional array, increasing memory allocation overhead. In general, if you need to sort an array and don't plan to modify it further, using `_.orderBy` is a better choice. However, if you're working with small arrays or need to sort the array in place, `_.sortBy` with `.reverse()` might be sufficient. **Lodash library:** Lodash is a popular JavaScript utility library developed by Isaac Schlueter. It provides a comprehensive set of functions for common tasks like array manipulation, string manipulation, and more. The `sortBy` and `orderBy` functions are part of Lodash's array utilities, making it easy to sort arrays in various ways. **Other considerations:** * In both tests, the array is populated with 100,000 random objects. * Both `_.sortBy` and `_.orderBy` use the "value" property for sorting. * The benchmark results are averaged over multiple executions per second (27.569528579711914 for _.sortBy and 26.994163513183594 for _.orderBy). **Other alternatives:** If you're not using Lodash, you can use native JavaScript functions like `sort()` or implement your own sorting algorithm. However, keep in mind that these alternatives might have performance overhead compared to optimized libraries like Lodash. In conclusion, the choice between `_.sortBy` with `.reverse()` and `_.orderBy` depends on your specific use case and requirements. By understanding the pros and cons of each approach, you can make an informed decision for your JavaScript application.
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?