Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sortby vs orderby
(version: 0)
Comparing performance of:
_.sortBy(arr) vs _.orderBy(arr)
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:
var arr = []
Tests:
_.sortBy(arr)
_.sortBy(arr)
_.orderBy(arr)
_.orderBy(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sortBy(arr)
_.orderBy(arr)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sortBy(arr)
6608033.5 Ops/sec
_.orderBy(arr)
7422738.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two functions from the Lodash library: `_.sortBy` and `_.orderBy`. Both functions are designed to sort an array of elements in a specific order. **What is being compared?** The benchmark tests how fast each function can execute on the same input data. Specifically, it measures the time it takes for each function to sort an empty array (`var arr = []`) using the Lodash library. **Options being compared:** There are two options being compared: 1. `_.sortBy(arr)`: This function uses a stable sorting algorithm that maintains the relative order of equal elements. The implementation is likely to be slower due to its stability guarantee. 2. `_.orderBy(arr)`: This function uses an unstable sorting algorithm that does not maintain the relative order of equal elements. The implementation might be faster, but it's less predictable in terms of the final sorted order. **Pros and Cons:** 1. **_.sortBy(arr)**: * Pros: Maintains the relative order of equal elements, which can be beneficial in certain scenarios (e.g., sorting by a secondary attribute). * Cons: May be slower due to its stability guarantee. 2. **_.orderBy(arr)**: * Pros: Can be faster, as it's an unstable sorting algorithm with fewer overheads. * Cons: Does not maintain the relative order of equal elements, which can lead to unpredictable results. **Lodash Library** The Lodash library is a popular utility belt for JavaScript developers. It provides a wide range of functions for tasks like array manipulation, object iteration, and more. In this benchmark, two functions from Lodash are being compared: `_.sortBy` and `_.orderBy`. **Other Considerations** When choosing between these two options, consider the following: * If stability is crucial (e.g., sorting by a secondary attribute), use `_.sortBy(arr)`. * If speed is more important, use `_.orderBy(arr)`. However, be aware of the potential for unpredictable results. **Alternative approaches** If you're not using Lodash or want to explore alternative approaches: 1. Use the built-in `Array.prototype.sort()` method, which is also implemented in JavaScript. 2. Consider using a third-party library like [fast-sort](https://github.com/fast-fast-sorted/fast-sort) or [sort-quick](https://github.com/kevansmith sort-quick), which are optimized for performance. Keep in mind that these alternatives might not provide the same level of stability guarantees as `_.sortBy(arr)` and may have different performance characteristics.
Related benchmarks:
_.SortBy vs Sort
orderBy vs .order
orderBy vs .order x3
order desc with lodash orderBy vs es6 sort method
Comments
Confirm delete:
Do you really want to delete benchmark?