Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Sort vs Array.sort vs Stable Sort
(version: 0)
Comparing performance of:
_.sortBy vs array.prototype.sort vs other vs Stable Sort
Created:
5 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
_.orderBy(arr,"value");
array.prototype.sort
arr.sort((a,b) => a.value - b.value);
other
var sort = function(array) { var len = array.length; if(len < 2) { return array; } var pivot = Math.ceil(len/2); return merge(sort(array.slice(0,pivot)), sort(array.slice(pivot))); }; var merge = function(left, right) { var result = []; while((left.length > 0) && (right.length > 0)) { if(left[0].value > right[0].value) { result.push(left.shift()); } else { result.push(right.shift()); } } result = result.concat(left, right); return result; }; sort(arr);
Stable Sort
const stableSort = (arr, compare) => arr .map((item, index) => ({item, index})) .sort((a, b) => compare(a.item, b.item) || a.index - b.index) .map(({item}) => item) stableSort(arr, (a,b) => a.value - b.value)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.sortBy
array.prototype.sort
other
Stable 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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sortBy
48.3 Ops/sec
array.prototype.sort
1236.0 Ops/sec
other
10.5 Ops/sec
Stable Sort
438.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different sorting algorithms is crucial in JavaScript development, as it can significantly impact application performance. The provided benchmark test on MeasureThat.net compares three distinct approaches to sort an array: Lodash's `orderBy` function (`_.sortBy`), the built-in `Array.prototype.sort` method, and a custom implementation using merge sort (`other`). **Option 1: Lodash's _.sortBy** * Purpose: To perform stable sorting on an array based on a specified attribute (in this case, `value`). * Pros: * Easy to use and integrate into existing codebases. * Provides stability guarantees, which is essential in certain scenarios where the order of equal elements matters. * Cons: * Performance might be slower compared to native JavaScript implementations due to the overhead of a library function. * Library: Lodash.js **Option 2: Array.prototype.sort** * Purpose: To sort an array in ascending or descending order based on one or more attributes (in this case, `value`). * Pros: * Native performance and lower memory usage compared to library functions like Lodash's `orderBy`. * Can be easily customized using a compare function. * Cons: * Returns an unstable sort, meaning the order of equal elements may not be preserved. * Might be slower for very large arrays due to its O(n log n) time complexity. **Option 3: Custom Merge Sort (other)** * Purpose: To implement a custom merge sort algorithm that can perform stable sorting on an array based on a specified attribute (`value`). * Pros: * Provides control over the implementation and allows for fine-tuning of performance characteristics. * Can be optimized further to reduce overhead and improve performance. * Cons: * Requires a deeper understanding of sorting algorithms and their implementations. **Stable Sort** * Purpose: To implement stable sorting using a modified merge sort algorithm that takes into account the index of each element. * Pros: * Provides stability guarantees, ensuring the order of equal elements is preserved. * Can be optimized for performance by reducing unnecessary comparisons. * Cons: * May have higher overhead compared to native JavaScript implementations like `Array.prototype.sort`. When choosing a sorting algorithm or library function, consider the trade-offs between performance, ease of use, and stability guarantees. In general: * Native JavaScript implementations (e.g., `Array.prototype.sort`) are suitable for most use cases where stability is not crucial. * Library functions like Lodash's `orderBy` provide convenience but might come at a cost in terms of performance. * Custom implementations or modified algorithms (like the merge sort example) offer control and potential performance improvements, but require more expertise. Other alternatives to consider: * **JavaScript native sort functions**: Some modern JavaScript engines have optimized native sort functions that can outperform Lodash's `orderBy` for certain use cases. * **Alternative libraries**: Other libraries like Underscore.js or Ramda provide similar functionality to Lodash but might offer different trade-offs in terms of performance, ease of use, and features. * **In-place sorting algorithms**: Algorithms like Insertion Sort or Selection Sort can be implemented in-place without requiring additional memory, which might be beneficial for very large datasets.
Related benchmarks:
Lodash sort vs array.prototype.sort - 2
Lodash sort vs array.prototype.sort vs basic sort vs Stable Sort 2
Lodash orderBy() vs array.prototype.sort
Lodash sort vs array.prototype.sort with localeCompare
Lodash sort vs array.prototype.sort 3
Comments
Confirm delete:
Do you really want to delete benchmark?