Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native sortBy
(version: 0)
Comparing performance of:
_.sortBy vs sortByKey
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:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.sortBy
_.sortBy(arr,"value");
sortByKey
const sortByKey = (array, key) => arr.sort((a,b) => (a[key] > b[key]) ? 1 : (a[key] < b[key]) ? -1 : 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sortBy
sortByKey
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sortBy
7.7 Ops/sec
sortByKey
1249422336.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. **Overview of the Benchmark** The provided benchmark compares two approaches for sorting an array in JavaScript: using the native `sort()` method and using the Lodash library's `sortBy()` function. **Native `sort()` Method** The native `sort()` method sorts an array by comparing the values of each element. It uses a stable sort algorithm, which means that if two elements have the same value, their original order is preserved. The native `sort()` method also has a time complexity of O(n log n) on average. **Lodash Library's `sortBy()` Function** The Lodash library provides a `sortBy()` function that sorts an array based on a specified key. It uses a stable sort algorithm and has a time complexity of O(n log n). The `sortBy()` function takes two arguments: the array to be sorted and the key function. **Comparison of Options** There are three options being compared: 1. **Native `sort()` Method**: This is the default sorting method in JavaScript. 2. **Lodash Library's `sortBy()` Function**: This is a custom sorting method that uses the Lodash library. 3. **`sortByKey` Custom Implementation**: This is a custom implementation of the sorting algorithm, which sorts an array based on a specified key. **Pros and Cons** Here are some pros and cons for each option: 1. **Native `sort()` Method** * Pros: + Fast and efficient + No additional library required * Cons: + Can be slower than custom implementations for large datasets + May not preserve the original order of equal elements 2. **Lodash Library's `sortBy()` Function** * Pros: + Convenient and easy to use + Preserves the original order of equal elements * Cons: + Requires an additional library installation + May be slower than custom implementations for large datasets 3. **`sortByKey` Custom Implementation** * Pros: + Fast and efficient + Can be optimized for specific use cases * Cons: + Requires writing custom code + May not preserve the original order of equal elements **Library: Lodash** The Lodash library is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string manipulation, and functional programming. In this benchmark, the `sortBy()` function from Lodash is used to compare with the native `sort()` method. **Special JS Feature/ Syntax** There are no special JavaScript features or syntax being tested in this benchmark. However, it's worth noting that some JavaScript engines may optimize certain aspects of the code differently, which could affect the results. **Alternatives** Some alternatives for sorting arrays in JavaScript include: 1. **ES6 `sort()` Method**: This is a modern and efficient way to sort arrays in JavaScript. 2. **`Array.prototype.sort()` with a custom compare function**: This allows developers to customize the sorting algorithm by providing a compare function. 3. **Other libraries**: There are other libraries available that provide optimized sorting algorithms, such as QuickSort or MergeSort. I hope this explanation helps!
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
lodash sortBy vs native sortBy 4.17.21
Lodash sortBy vs orderBy performance
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?