Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native [immutable6] sortBy
(version: 0)
Comparing performance of:
_.sortBy vs sortByKey
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:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value:getRandomInt(100000)}); }
Tests:
_.sortBy
_.sortBy(arr,"value");
sortByKey
const sortByKey = (array, key) => arr.concat().sort((a,b) => a[key] - b[key]); sortByKey(arr,"value");
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:
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):
I'll break down the benchmark test for you. **What is being tested?** The benchmark tests two different approaches to sort an array of objects in descending order based on the value of a specific property: "value". The arrays are created with random integers between 0 and 100,000. **Options compared:** 1. **Lodash's `sortBy` function**: A popular JavaScript utility library that provides various functions for manipulating data. In this case, it uses the `sort()` method to sort the array based on a custom comparison function. 2. **Native/ES6 `Array.prototype.sort()` method with a callback function**: The native `Array.prototype.sort()` method allows sorting arrays using a compare function. Here, a callback function is used to extract the "value" property from each object in the array. **Pros and Cons:** 1. **Lodash's `sortBy` function**: * Pros: + Well-tested and widely used library. + Provides additional utility functions that might be useful in other parts of the code. * Cons: + Adds an external dependency (the Lodash library). + May introduce overhead due to the size of the library and the need to load it. 2. **Native/ES6 `Array.prototype.sort()` method with a callback function**: * Pros: + No additional dependencies or overhead. + Utilizes the native implementation, which is typically optimized for performance. * Cons: + Requires knowledge of JavaScript's sort() method and its callback functions. In general, using a library like Lodash can simplify code and provide convenience, but it may also introduce unnecessary dependencies. On the other hand, utilizing the native `Array.prototype.sort()` method with a callback function is more lightweight but requires manual implementation of the comparison logic. **Library usage:** The benchmark uses Lodash's `sortBy` function to sort an array in descending order based on the "value" property. The library is loaded from a CDN (Content Delivery Network) using the `Html Preparation Code`. **Special JS feature/syntax:** None mentioned explicitly, but the use of ES6 syntax (e.g., arrow functions, template literals) and the `const` keyword for variable declarations are part of modern JavaScript. **Other alternatives:** If you wanted to compare other sorting algorithms or approaches, some possible alternatives could include: * Using a different sorting algorithm, such as quicksort or mergesort. * Implementing a custom sorting function using recursion or iteration. * Comparing the performance of other libraries that provide similar functionality (e.g., Moment.js for date manipulation). * Measuring the performance of different JavaScript engines or interpreters. Please note that the specific alternatives would depend on the goals and requirements of the benchmark.
Related benchmarks:
Lodash sort vs array.prototype.sort - 2
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
lodash sortBy vs native sortBy 4.17.21
Lodash sort vs array.prototype.sort with localeCompare
Comments
Confirm delete:
Do you really want to delete benchmark?