Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash - sortBy vs orderBy
(version: 0)
Lodash - sortBy vs orderBy
Comparing performance of:
_.sortBy vs _.orderBy
Created:
5 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sortBy
11.5 Ops/sec
_.orderBy
11.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Overview** The benchmark compares two methods for sorting an array of objects: `lodash.sortBy` and `lodash.orderBy`. The goal is to determine which method performs better in terms of execution time. **Options Compared** Two options are compared: 1. **_.sortBy(arr, "value")**: This method sorts the array `arr` based on the value property of each object. 2. **_.orderBy(arr, "value")**: This method also sorts the array `arr` based on the value property of each object, but with a slight difference in implementation. **Pros and Cons** Both methods are implemented in Lodash, a popular JavaScript utility library. Here's a brief overview of their differences: * **_.sortBy(arr, "value")**: This method uses a stable sort algorithm (e.g., Timsort) to ensure that equal elements maintain their original order. It also allows for a custom compare function. * **_.orderBy(arr, "value")**: This method uses an unstable sort algorithm (e.g., QuickSort) and does not guarantee the preservation of equal elements' original order. Pros of _.sortBy: * Stable sorting * Customizable comparison function Cons of _.sortBy: * May be slower due to stable sorting * Requires a custom compare function Pros of _.orderBy: * Faster execution time (unstable sorting) * Simpler implementation Cons of _.orderBy: * Unstable sorting, which may lead to equal elements losing their original order * Less customizable than _.sortBy **Library and Purpose** Lodash is a JavaScript utility library that provides a collection of functions for common tasks, such as array manipulation, object manipulation, and more. In this case, Lodash's `_.sortBy` and `_.orderBy` functions are used to sort arrays based on specific properties. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax mentioned in the benchmark JSON. However, it's worth noting that Lodash uses a functional programming style, which can be beneficial for readability and maintainability. **Other Alternatives** If you need to implement sorting functionality without using a library like Lodash, you could consider the following alternatives: * Built-in JavaScript methods: `Array.prototype.sort()`, `Array.prototype.reverse()` * External libraries: Such as `moment.js` (for date-based sorting), `lodash-es` (a lighter-weight alternative to Lodash) Keep in mind that these alternatives may have different performance characteristics and use cases compared to using a dedicated library like Lodash.
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?