Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
order desc with lodash orderBy vs es6 sort method
(version: 0)
Comparing performance of:
lodash sort method vs es6 sort method
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 array = [ { 'name': 'lim', 'age': 26 }, { 'name': 'kim', 'age': 28 }, { 'name': 'choi', 'age': 32 }, { 'name': 'park', 'age': 21 } ];
Tests:
lodash sort method
var result = _.orderBy(array, ['age'], ['desc']);
es6 sort method
var result = array.sort((prev, cur) => cur.age - prev.age);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash sort method
es6 sort method
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 world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided benchmark compares two approaches to sorting an array in descending order: Lodash's `orderBy` function and the built-in ECMAScript 6 (ES6) `sort` method. The benchmark is designed to measure the performance difference between these two methods, which are commonly used for data processing and manipulation. **Options Compared** Two options are compared: 1. **Lodash's `orderBy` function**: This is a utility function that takes an array and specifies one or more keys to sort on, as well as the direction of sorting (ascending or descending). In this benchmark, it is used with only one key (`'age'`) in ascending order. 2. **ES6 `sort` method**: This is a built-in method for sorting arrays in JavaScript. It takes a comparison function as an argument, which defines the sorting logic. **Pros and Cons of Each Approach** 1. **Lodash's `orderBy` function**: * Pros: + Easy to use: simply pass the array and the keys to sort on. + Flexible: supports multiple sorting keys and directions. * Cons: + Adds overhead due to the need to create a new array with the sorted elements (not a direct in-place modification). + May have slower performance compared to other methods, especially for large arrays. 2. **ES6 `sort` method**: * Pros: + In-place sorting: modifies the original array without creating a new one. + Can be faster than using a library like Lodash for small to medium-sized arrays. * Cons: + More complex to use, requiring a comparison function that defines the sorting logic. + Not as flexible as Lodash's `orderBy` function, which supports multiple keys and directions. **Library Used** The benchmark uses the Lodash library, which is a popular utility library for JavaScript. In this case, it is used specifically for its `orderBy` function, which provides a simple way to sort arrays based on one or more keys. **Special JS Feature/ Syntax** There are no special JavaScript features or syntax used in this benchmark. The only notable aspect is the use of the ES6 `sort` method, which is a standard feature in modern JavaScript environments. **Other Alternatives** Other alternatives for sorting arrays include: 1. **Array.prototype.slice() + Array.prototype.sort()**: This approach involves creating a copy of the original array using `slice()` and then sorting it using the `sort()` method. 2. **Array.prototype.map() + Array.prototype.every()**: This approach involves mapping each element in the array to itself (using `map()`) and then checking if the resulting array is sorted correctly (using `every()`). 3. **External sorting libraries**: There are several external libraries available that provide optimized sorting algorithms, such as [FastSort](https://fastsort.io/) or [Duniter](http://duniter.org/). These alternatives may offer better performance for specific use cases or large datasets, but they often come with additional overhead and complexity.
Related benchmarks:
sortby vs orderby
sortBy performance1
lodash vs es6 in sort method
native sort of objects by linus
Comments
Confirm delete:
Do you really want to delete benchmark?