Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native [immutable] 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(100)}); }
Tests:
_.sortBy
_.sortBy(arr,"value");
sortByKey
const sortByKey = (array, key) => arr.concat().sort((a,b) => (a[key] > b[key]) ? 1 : (a[key] < b[key]) ? -1 : 0);
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 break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined by two test cases: `_.sortBy` from Lodash and a custom implementation called `sortByKey`. The goal is to compare the performance of these two approaches in sorting an array of objects. **Test Cases** 1. **_sortBy**: This test case uses the `lodash.sortBy` function, which sorts the array in ascending order based on the provided key ("value" in this case). Lodash provides a robust and efficient implementation of various sorting algorithms. 2. **sortByKey**: This is the custom implementation that sorts the array using the `sort` method with a compare function. It concatenates the original array, creates a new sorted array, and then discards the original array. This approach uses a simple but inefficient algorithm. **Options Compared** The benchmark compares two options: 1. **Lodash's _.sortBy**: A well-tested and optimized implementation of the sorting algorithm. 2. **Custom sortByKey**: An alternative implementation that uses a simple but inefficient algorithm. **Pros and Cons of Each Approach** * **_lodash.sortBy_**: + Pros: - Well-tested and optimized - Handles edge cases (e.g., duplicates, null/undefined values) - Provides a consistent sorting order + Cons: - May have additional overhead due to the use of an external library * **sortByKey**: + Pros: - Custom implementation may be optimized for specific use cases + Cons: - Inefficient algorithm (concatenating and discarding the original array) - May not handle edge cases correctly **Library Used** The `lodash.sortBy` function uses a sorting algorithm called "stable sort" or "Timsort", which is a hybrid of merge sort and insertion sort. Timsort is known for its efficiency, stability, and simplicity. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark that would require specific knowledge or expertise to understand. **Other Alternatives** If you're interested in exploring alternative sorting algorithms or implementations, some popular options include: * **Native Array.sort()**: This is a simple and efficient algorithm built into the JavaScript engine. * **Ramda's sortBy**: Ramda is another popular library for functional programming in JavaScript. Its `sortBy` function uses a similar stable sort algorithm as Lodash. * **Custom sorting algorithms**: Depending on your specific requirements, you may want to explore custom sorting algorithms like quicksort, mergesort, or heapsort. Keep in mind that the choice of sorting algorithm or implementation depends on the specific use case and performance requirements.
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?