Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sortBy vs orderBy corrected vs sort
(version: 0)
Lodash sortBy vs orderBy
Comparing performance of:
_.sortBy vs _.orderBy vs sort
Created:
4 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");
sort
arr.sort((a, b) => a - b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.sortBy
_.orderBy
sort
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 dive into the benchmark. **Benchmark Overview** The benchmark tests three different approaches to sort an array of objects in JavaScript: 1. Lodash `sortBy` 2. Lodash `orderBy` (corrected implementation) 3. Built-in `sort` **Lodash `sortBy` and `orderBy`** Both `sortBy` and `orderBy` are used to sort arrays, but they have different performance characteristics. * **Lodash `sortBy`:** This method sorts the array in-place based on a specified "function" that defines the sorting order. The function takes two arguments (the value from each object being compared) and returns a negative value if the first value is less than the second, zero if they're equal, or a positive value otherwise. * **Lodash `orderBy`:** This method sorts the array in-place based on an existing property of each object. In this case, it's sorting by the "value" property. The difference between these two methods lies in their implementation complexity and efficiency. Lodash provides optimized implementations for both cases. However, due to some issues with the library implementation, there is a potential overhead introduced when using `orderBy` that can cause performance degradation in comparison to `sortBy`. **Built-in `sort`** This method sorts the array in-place based on an optional "compareFunction" that defines how to compare elements. * **Performance:** The built-in `sort` method has better performance characteristics than Lodash's sorting methods. This is because it does not require additional function calls and can use a simple comparison function, which makes it faster. * **Implementation Complexity:** However, the built-in implementation requires a custom comparison function for the array items. **Comparison of Options** | Option | Pros | Cons | | --- | --- | --- | | Lodash `sortBy` | Optimized implementation with better performance in most cases. Less chance of errors due to less code execution. | Potential overhead due to some issues with the library implementation. The function takes two arguments, which could lead to unexpected behavior if used with a different type. | | Lodash `orderBy` (corrected implementation) | Optimized for sorting by an existing property in the object. Smaller chance of errors compared to `sortBy`. | Potential performance degradation due to incorrect or inefficient comparison logic (due to its dependency on the internal library's implementation). More complex due to the extra function call introduced by this method. | Built-in `sort` | Better performance, uses optimized algorithms, minimal execution of custom code. | Requires a custom comparison function for array items, more likely to be error-prone if not implemented carefully. **Library and Special JS Features** * Lodash: A popular JavaScript utility library that provides functional programming helpers. * Lodash's sorting methods (`sortBy` and `orderBy`) utilize their own optimized algorithms. They take advantage of the native JavaScript `Array.prototype.sort()` method but provide extra benefits in terms of readability, maintainability, and reliability. Overall, the choice between these options depends on your specific use case and performance requirements: * If you're dealing with a large dataset or need to sort objects by multiple properties, using Lodash's sorting methods (`sortBy` and `orderBy`) might be the best option. * If you prefer a lightweight implementation without any additional dependencies, the built-in `sort()` method is recommended.
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?