Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native [immutable4] 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):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Overview** The benchmark compares two approaches for sorting an array of objects: using the `lodash` library's `sortBy` function and implementing a custom sorting function, `sortByKey`. The test case generates a large array of 100,000 objects with random values between 0 and 100,000. **Options Compared** Two options are compared: 1. **Lodash's `sortBy` function**: This is a popular utility function that sorts an array of objects based on a specified key. 2. **Custom sorting function, `sortByKey`**: This implementation creates a new sorted array by concatenating the original array with its own copy, sorting it using the `sort()` method, and then applying a callback function to extract the desired value. **Pros and Cons** **Lodash's `sortBy` function:** Pros: * Well-tested and widely adopted library * Efficient implementation using binary search * Handles edge cases and optimizations for large datasets Cons: * Requires importing an external library (lodash) * May introduce overhead due to the library's initialization and garbage collection * Limited control over the sorting algorithm and key extraction **Custom sorting function, `sortByKey`:** Pros: * No external dependencies or imports * More direct control over the sorting algorithm and key extraction * Can be optimized for specific use cases or performance requirements Cons: * Requires manual implementation of the sorting algorithm and key extraction logic * May lead to code bloat or increased complexity due to the custom implementation * Less well-tested and less widely adopted compared to lodash's `sortBy` function **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. The `sortBy` function is one of its most commonly used utilities, allowing developers to sort arrays of objects based on specific keys. In the benchmark, the `lodash` library's `sortBy` function is used to compare its performance with the custom implementation, `sortByKey`. This allows users to evaluate both options and choose the best approach for their specific use case. **Special JS Feature/Syntax** There doesn't appear to be any special JavaScript features or syntax that are being exploited in this benchmark. The focus is on comparing two different sorting approaches rather than leveraging advanced JavaScript techniques. **Alternatives** If you're interested in exploring alternative sorting algorithms or libraries, here are a few options: * **Built-in `Array.prototype.sort()` method**: This is the most basic and widely supported sorting algorithm in JavaScript, which uses a stable sort (Timsort) for arrays. * **Other utility libraries**: Libraries like Moment.js, React Query, or other array manipulation utilities may offer more advanced sorting features or optimizations for specific use cases. * **Custom sorting algorithms**: Implementing your own sorting algorithm using techniques like quicksort, mergesort, or heapsort can provide fine-grained control over performance and complexity. I hope this explanation helps you understand the benchmark and its comparisons!
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?