Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native sortBy 4.17.21
(version: 0)
Comparing performance of:
_.sortBy vs sortByKey
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/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.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 provided benchmark and its test cases. **Benchmark Overview** The benchmark compares two approaches for sorting an array of objects: `lodash.sortBy` and a custom implementation using a lambda function (`sortByKey`). The goal is to measure which approach performs better in terms of execution time. **Lodash Library** Lodash (a popular JavaScript library) provides a utility function `sortBy` that can be used to sort an array based on a specified key. In this benchmark, the `lodash.sortBy` function is used with the default implementation. **Custom Implementation: sortByKey** The custom implementation (`sortByKey`) sorts an array by comparing each element's property value using a lambda function. This approach allows for more control over the sorting process and can be beneficial if the sorting logic needs to be customized. **Pros and Cons of Each Approach** 1. **Lodash `sortBy`**: * Pros: + Built-in implementation with optimized performance + Reduces boilerplate code * Cons: + Less control over the sorting process + May not perform well for large or complex datasets 2. **Custom Implementation (`sortByKey`)**: * Pros: + More control over the sorting process + Can be beneficial for large or complex datasets * Cons: + Requires more boilerplate code (lambda function) + May not perform as well as optimized library implementations **Other Considerations** When choosing between these approaches, consider the following factors: * **Data complexity**: If your dataset is simple and doesn't require advanced sorting logic, `lodash.sortBy` might be sufficient. For more complex datasets or custom sorting requirements, `sortByKey` might be a better choice. * **Performance**: If execution speed is crucial, optimized library implementations like `lodash.sortBy` might provide better performance than the custom implementation (`sortByKey`). * **Code readability and maintainability**: The custom implementation requires more code and can make it harder to understand for some developers. In contrast, `lodash.sortBy` provides a concise and readable way to sort arrays. **Alternatives** Other alternatives for sorting arrays in JavaScript include: * Using the built-in `Array.prototype.sort()` method with a compare function * Implementing a custom sorting algorithm (e.g., merge sort, quicksort) * Utilizing libraries like `underscore.js` or `moment.js` for more advanced sorting capabilities Keep in mind that each approach has its strengths and weaknesses, and choosing the right one depends on your specific use case and requirements.
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
Lodash sortBy vs orderBy performance
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?