Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_orderBy vs javascript arr.sort
(version: 0)
Comparing performance of:
_.sortBy vs _.orderBy vs JS 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:
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");
_.orderBy
_.orderBy(arr,"value", "asc");
JS sort
arr.sort((a,b) => a.value - b.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.sortBy
_.orderBy
JS 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 break down the benchmark and analyze what's being tested. **What is being tested?** The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares three approaches for sorting an array of objects with a numerical "value" property: Lodash's `_.sortBy`, `_.orderBy`, and the built-in JavaScript array method `sort`. **Options compared:** 1. **Lodash's `_sortBy`**: This function takes an array and returns a new sorted array, but unlike the other two options, it doesn't modify the original array. It uses a stable sorting algorithm to maintain the relative order of equal elements. 2. **Lodash's `_orderBy`**: Similar to `_.sortBy`, this function sorts the array based on a specific key (in this case, "value") and returns a new sorted array without modifying the original one. However, it allows for ascending or descending ordering by specifying the second argument ("asc" or "-asc"). 3. **JavaScript's built-in `sort` method**: This function modifies the original array and sorts it in-place using a stable sorting algorithm. **Pros and Cons:** * **Lodash's `_sortBy`**: * Pros: * Provides a stable sorting algorithm. * Does not modify the original array, making it suitable for preserving data integrity. * Cons: * Requires an external library (Lodash). * May have a performance overhead due to the need to import and initialize the Lodash library. * **Lodash's `_orderBy`**: * Pros: * Allows for ascending or descending ordering, making it more flexible than JavaScript's built-in `sort`. * Provides stability like Lodash's `_sortBy`. * Cons: * Requires an external library (Lodash). * May have a performance overhead due to the need to import and initialize the Lodash library. * **JavaScript's built-in `sort` method**: * Pros: * No additional libraries or dependencies required. * Can be optimized for specific use cases, like merging multiple arrays or sorting objects based on multiple keys. * Cons: * May not maintain the relative order of equal elements (non-stable sort). * Modifies the original array, potentially affecting data integrity. **Library used:** Lodash is a popular JavaScript utility library that provides a comprehensive set of functions for various tasks, including sorting. The `_.sortBy` and `_.orderBy` functions are part of Lodash's functional programming utilities. **Special JS feature or syntax:** None mentioned in this benchmark. **Other alternatives:** If you want to implement your own sorting algorithm or use an alternative library, some options include: * **QuickSort**: A divide-and-conquer algorithm for sorting arrays. * **Merge Sort**: Another divide-and-conquer algorithm for sorting arrays. * **Heap Sort**: A comparison-based sorting algorithm that uses a heap data structure. * **Algorithm X**: A proprietary sorting algorithm developed by Google, which is considered to be one of the fastest and most efficient algorithms. Keep in mind that implementing an optimized sorting algorithm requires a good understanding of computer science concepts and performance optimization techniques.
Related benchmarks:
Lodash orderBy vs array.prototype.sort
Lodash orderBy() vs array.prototype.sort
Lodash orderBy vs array.prototype.sort fork
Lodash orderBy (fn) vs array.prototype.sort
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?