Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash orderBy vs Array.sort
(version: 0)
Sort and array of objects by 2 properties : - text: descending - id: ascending
Comparing performance of:
_.orderBy vs Array.sort
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 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', 'id'], ['desc', 'asc'])
Array.sort
arr.sort((a, b) => (a.text < b.text) ? 1 : (a.text === b.text) ? ((a.id > b.id) ? 1 : -1) : -1 )
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.orderBy
19700.6 Ops/sec
Array.sort
289693.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down what's being tested in this benchmark. **What is being compared?** The benchmark compares two different approaches to sorting an array of objects: 1. Using the `Array.sort()` method with a custom comparison function. 2. Using the `_orderBy` function from the Lodash library (version 4.17.5). **What are we sorting?** We're sorting an array of objects, where each object has two properties: `id` and `text`. The benchmark is specifically sorting this array in descending order by `text` and ascending order by `id`. **The options compared:** 1. **Array.sort()**: This method sorts the array using a custom comparison function that takes two objects (`a` and `b`) as input. The function returns a value that determines the sort order: * If `a.text < b.text`, return 1 (descending order). * If `a.text === b.text`, compare the `id` values: if `a.id > b.id`, return 1 (ascending order), otherwise return -1. 2. **_.orderBy**: This function from Lodash takes an array, a list of properties to sort by (`['text', 'id']`), and a list of sort orders (`['desc', 'asc']`) as input. It returns the sorted array. **Pros/Cons:** Here are some general pros and cons of each approach: 1. **Array.sort()**: * Pros: built-in, simple to use. * Cons: can be slower for complex sorting tasks (like comparing objects with multiple properties), requires a custom comparison function. 2. **_.orderBy**: * Pros: more flexible, allows for easy specification of sort orders and properties. * Cons: requires an external library (Lodash), may have performance overhead due to the library's own implementation. **Other considerations:** When deciding which approach to use, consider factors like: 1. Performance requirements: If you need to sort large datasets or perform sorting frequently, using a specialized library like Lodash might be beneficial. 2. Code complexity: If your codebase is already using Lodash, it might be more convenient to stick with _.orderBy for consistency. **Library and its purpose:** The Lodash library (version 4.17.5) provides utility functions for JavaScript development. In this case, the `_orderBy` function simplifies the process of sorting arrays by multiple properties with custom orders. **JS feature or syntax used:** None specific to this benchmark are mentioned. **Alternatives:** If you don't want to use Lodash, alternative approaches to sorting arrays could be: 1. Using a custom comparison function with Array.prototype.sort(). 2. Implementing your own sorting algorithm (e.g., quicksort or mergesort). 3. Utilizing other JavaScript libraries or frameworks that provide sorting functionality. Hope this explanation helps!
Related benchmarks:
sortby vs orderby
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
lodash orderBy vs Array.sort on single key
Comments
Confirm delete:
Do you really want to delete benchmark?