Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash SortBy Vs Stable Sort
(version: 0)
Comparing performance of:
Lodash sortBy vs Stable Sort
Created:
2 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({timestamp:getRandomInt(100), nanos:getRandomInt(100)}); }
Tests:
Lodash sortBy
_.sortBy(arr, ['timestamp', 'nanos'])
Stable Sort
const stableSort = (data) => data .map((item, index) => ({ item, index })) .sort( (a, b) => a.item.timestamp - b.item.timestamp || a.item.nanos - b.item.nanos || a.index - b.index, ) .map(({ item }) => item); stableSort(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash sortBy
Stable Sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash sortBy
18.8 Ops/sec
Stable Sort
30.8 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 Definition** The benchmark is comparing two sorting algorithms: `_.sortBy` from Lodash and a custom implementation called `stableSort`. **Lodash _.sortBy** `.sortBy(arr, ['timestamp', 'nanos'])` uses the Lodash library to sort an array of objects based on two properties: `timestamp` and `nanos`. The `_` symbol is a shortcut for accessing Lodash functions. The pros of using Lodash's `_.sortBy` include: * Convenience: It provides a simple way to sort arrays without having to write custom sorting code. * Performance: Lodash's implementation is optimized for performance, making it suitable for large datasets. However, the cons include: * Overhead: Using an external library may introduce some overhead due to the need to load and initialize the library. * Dependency: The benchmark relies on the Lodash library being available, which might not always be the case. **Stable Sort** The `stableSort` implementation is a custom sorting algorithm that sorts an array of objects based on multiple criteria. It first creates an intermediate array with both the original object and its index, then sorts this array based on a custom comparison function. Finally, it maps back to retrieve only the sorted objects. The pros of using `stableSort` include: * Customizability: It allows developers to tailor the sorting behavior to their specific needs. * No dependencies: This implementation doesn't rely on any external libraries or frameworks. However, the cons include: * Complexity: The custom implementation requires more code and may be harder to understand for those unfamiliar with sorting algorithms. * Potential performance issues: Depending on the implementation details, `stableSort` might not be as efficient as Lodash's optimized `_sortBy`. **Other Considerations** When choosing between these two approaches, consider the following factors: * Development time and complexity * Performance requirements (e.g., handling large datasets) * Maintainability and readability of the code **Library: Lodash.js** Lodash is a popular JavaScript library that provides a wide range of utility functions for tasks like array manipulation, object transformation, and more. Its `_.sortBy` function is just one example of its many sorting algorithms. In this benchmark, Lodash's `_sortBy` is used to sort an array based on multiple criteria, making it suitable for scenarios where simple sorting is not sufficient. However, when custom sorting requirements are needed, `stableSort` provides a more flexible and control-oriented solution. **Special JS Features** There are no special JavaScript features or syntax mentioned in this benchmark. The focus remains on the two sorting algorithms being compared: Lodash's `_.sortBy` and the custom implementation called `stableSort`.
Related benchmarks:
Check speed of lodash sort vs js sort
Lodash sort vs array.prototype.sort - 2
Lodash orderBy() vs array.prototype.sort
lodash sortBy vs native sortBy 4.17.21
Comments
Confirm delete:
Do you really want to delete benchmark?