Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
orderby vs sortby vs JS-sort
(version: 4)
Comparing performance of:
sortby vs orderby vs plain JS vs ES2015
Created:
6 years ago
by:
Registered User
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 < 1000; i++){ arr.push({value:getRandomInt(1000)}); }
Tests:
sortby
_.sortBy(arr,"value");
orderby
_.orderBy(arr,"value", ['asc']);
plain JS
arr.sort(function(a, b) { return a.value - b.value; });
ES2015
arr.sort((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
orderby
plain JS
ES2015
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 benchmarks on MeasureThat.net. **Benchmark Overview** The benchmark compares the performance of three approaches for sorting an array of objects in ascending order: 1. **Lodash `.sortBy()`**: A utility function from the Lodash library that sorts an array based on a specified key. 2. **Moment.js `.orderBy()`**: A method from Moment.js, a popular date and time library, that sorts an array based on a specified key. 3. **Plain JavaScript `arr.sort()`**: A built-in Array.prototype.sort() method that sorts an array using a compare function. **Options Compared** The benchmark compares the performance of these three approaches: * `_.sortBy(arr,"value")`: Uses Lodash's `.sortBy()` function to sort the array based on the "value" property. * `_.orderBy(arr,"value", ['asc'])`: Uses Moment.js's `.orderBy()` method to sort the array in ascending order (asc) based on the "value" property. * `arr.sort(function(a, b) { return a.value - b.value; })`: Uses plain JavaScript's Array.prototype.sort() method with a custom compare function to sort the array based on the "value" property. * `arr.sort((a, b) => a.value - b.value)`: Uses a newer version of the Array.prototype.sort() method with an arrow function (introduced in ECMAScript 2015) to sort the array based on the "value" property. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Lodash `.sortBy()`**: Pros: + Easy to use and readable. + Provides a consistent API for sorting arrays. Cons: + Adds an external dependency (Lodash). + May be slower due to the overhead of loading an additional library. * **Moment.js `.orderBy()`**: Pros: + Built on top of Lodash, so it shares some dependencies. + Provides a robust and flexible way to handle date/time comparisons. Cons: + Requires an additional dependency (Moment.js). + May be overkill for simple sorting tasks like this one. * **Plain JavaScript `arr.sort()`**: Pros: + Fastest approach due to the use of native Array.prototype.sort(). + Does not add any external dependencies. Cons: + Requires a custom compare function, which can make the code harder to read and maintain. **Library Used** Lodash is a popular utility library that provides a collection of functions for common tasks like sorting, filtering, and mapping. In this benchmark, Lodash's `.sortBy()` function is used to sort the array based on the "value" property. Moment.js is another widely-used library that provides date and time functionality. While it's not strictly necessary for this specific benchmark (since we're only sorting an array of objects with numeric values), Moment.js is often used in conjunction with Lodash, which is why _.orderBy() is also available as an option. **Special JS Features** The benchmark uses a new feature introduced in ECMAScript 2015: arrow functions. The last two options for the `arr.sort()` method use an arrow function to define the compare function. This syntax provides several benefits, including: * Improved readability: Arrow functions eliminate the need for explicit function declarations. * Reduced memory allocation: Arrow functions do not create a new scope, which reduces memory overhead. Overall, this benchmark provides a useful comparison of three sorting approaches in JavaScript, highlighting the trade-offs between different libraries and native methods.
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 small array
Comments
Confirm delete:
Do you really want to delete benchmark?