Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash orderBy vs Array.sort on single key
(version: 0)
Sort and array of objects by 2 properties : - text: descending - id: ascending
Comparing performance of:
_.orderBy vs Array.sort
Created:
3 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 generateText = function(){ return Math.random().toString(36).substring(2, 4); } var arr = [] for(i=0; i<100; i++){ const obj = {} obj.id = i; obj.text = generateText() arr.push(obj) }
Tests:
_.orderBy
_.orderBy(arr, ['text'], ['desc'])
Array.sort
arr.sort((a, b) => a.text - b.text)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.orderBy
Array.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 Definition JSON and explain what's being tested. **Overview** The benchmark is designed to compare the performance of two sorting algorithms: Lodash's `_.orderBy` and Array.prototype's `sort`. The test case generates an array of 100 objects with random "text" and "id" properties, and then sorts the array in descending order by "text" and ascending order by "id". **Options being compared** The benchmark is comparing two options: 1. **Lodash's `_.orderBy`**: This function takes an array and a key (in this case, "text"), as well as a descriptor for the sorting (in this case, descending for "text" and ascending for "id"). It returns a new sorted array. 2. **Array.prototype's `sort`**: This is a built-in JavaScript method that sorts the elements of an array in place based on a compare function. **Pros and cons** Here are some pros and cons of each approach: * Lodash's `_.orderBy`: + Pros: Easy to use, provides a convenient interface for sorting arrays with multiple keys. + Cons: Requires an external library (Lodash), may have overhead due to the extra dependency. * Array.prototype's `sort`: + Pros: Built-in method, no additional dependencies required. Can be more efficient in certain cases since it doesn't require an extra function call. + Cons: More verbose to use, requires a compare function that can handle multiple keys. **Library and its purpose** In this benchmark, Lodash is used as a library for providing the `_.orderBy` method. The `lodash.min.js` file is included in the HTML preparation code using a script tag. Lodash provides a convenient way to perform various tasks, including sorting arrays with multiple keys. **Special JS feature or syntax** There are no special JavaScript features or syntax used in this benchmark. It only relies on standard JavaScript methods and libraries. **Other alternatives** If you wanted to write this benchmark from scratch without using an external library like Lodash, you could use a custom implementation of the `_.orderBy` method or implement the sorting logic yourself. Some other alternatives for sorting arrays include: * Using a different JavaScript library that provides similar functionality (e.g., Moment.js for date-based sorting) * Implementing a sorting algorithm from scratch using a data structure like a heap or a balanced tree * Using a parallel processing framework to compare the performance of different sorting algorithms Keep in mind that writing a benchmark from scratch can be more complex and may require more expertise in JavaScript and computer science.
Related benchmarks:
sortby vs orderby
lodash orderBy vs Array.sort
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
Comments
Confirm delete:
Do you really want to delete benchmark?