Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.sortBy vs _.orderBy vs JS sort
(version: 0)
Comparing performance of:
Native JS sort vs lodash sortby vs lodash orderBy
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.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Script Preparation code:
var arr = []; for (var i = 0; i < 1_000_000; i++) { arr.push({ value: Math.random() }); }
Tests:
Native JS sort
arr.sort((a, b) => a.value - b.value);
lodash sortby
_.sortBy(arr, (arr) => arr.value);
lodash orderBy
_.orderBy(arr, (arr) => arr.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Native JS sort
lodash sortby
lodash orderBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native JS sort
15.1 Ops/sec
lodash sortby
7.4 Ops/sec
lodash orderBy
4.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Benchmark Overview** The benchmark is designed to compare the performance of three sorting algorithms: 1. Native JavaScript `sort()` function 2. Lodash's `_.sortBy()` function 3. Lodash's `_.orderBy()` function **Options Compared** The main options being compared are: * **Native JavaScript `sort()`**: This is a built-in function in JavaScript that sorts an array in ascending order. * **Lodash's `_.sortBy()`**: This function uses a stable sort algorithm (e.g., Timsort or merge sort) to sort an array based on a custom comparison function. In this case, the comparison function is `(a, b) => a.value - b.value`, which sorts objects by their `value` property. * **Lodash's `_.orderBy()`**: This function is similar to `_.sortBy()`, but it only returns the first occurrence of each sorted value (i.e., if there are duplicates, only one will be returned). In this case, the comparison function is also `(a, b) => a.value - b.value`. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Native JavaScript `sort()`**: + Pros: Fast, widely supported, and easy to implement. + Cons: Unstable sorting (i.e., may change order for arrays with duplicate values), and can be slow for very large datasets. * **Lodash's `_.sortBy()`**: + Pros: Stable sorting, efficient implementation, and can handle large datasets. + Cons: Requires the Lodash library to be included, and its performance may vary depending on the specific sorting algorithm used. * **Lodash's `_.orderBy()`**: + Pros: Similar benefits to `_.sortBy()`, with an added benefit of returning only unique values. + Cons: Same drawbacks as `_.sortBy()`, plus it may return fewer results than expected if there are duplicates. **Library and Syntax** The Lodash library is used in this benchmark, specifically the `lodash.min.js` file. The `_.sortBy()` and `_.orderBy()` functions are part of Lodash's utility belt, providing various functional programming helpers. No special JavaScript features or syntax are being tested in this benchmark, as it only uses standard JavaScript syntax for the sorting algorithms. **Other Alternatives** If you're interested in exploring alternative sorting algorithms, here are a few options: * **Quicksort**: A fast and efficient sorting algorithm that's suitable for large datasets. * **Merge sort**: A stable sorting algorithm that's well-suited for large datasets with many duplicate values. * **Radix sort**: A non-comparative sorting algorithm that's optimized for sorting integers or strings. Keep in mind that the choice of sorting algorithm ultimately depends on your specific use case, dataset size, and performance requirements.
Related benchmarks:
_.sortBy vs JS sort
Lodash sortBy vs orderBy performance
_.sortBy vs _.orderBy vs JS sort multiple
Compount Mapping and Sorting (lodash vs native)
Comments
Confirm delete:
Do you really want to delete benchmark?