Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native [immutable5] 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].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 break down the provided benchmark and explain what is tested, compared options, pros and cons of those approaches, and other considerations. **Benchmark Overview** MeasureThat.net provides a JavaScript microbenchmarking platform where users can create and run benchmarks. In this case, we have a benchmark that compares two ways to sort an array of objects using the "value" key: `lodashsortBy` (using the Lodash library) and the native implementation with an anonymous function (`sortByKey`). The benchmark consists of creating an array of 100,000 random objects and then sorting it using each approach. **Library Used** The benchmark uses the Lodash library, which is a popular JavaScript utility library that provides a lot of useful functions for tasks such as data manipulation, string manipulation, and more. In this case, `lodash.sortBy` is used to sort the array of objects. **Native Implementation (`sortByKey`)** The native implementation uses an anonymous function to compare objects in the array: ```javascript const sortByKey = (array, key) => [...arr].sort((a,b) => a[key] - b[key]); ``` This implementation is straightforward and relies on the built-in `Array.prototype.sort()` method. **Pros and Cons** Native implementation (`sortByKey`): Pros: * Fast and efficient * Built-in support for sorting arrays Cons: * Requires an explicit comparison function (in this case, `a[key] - b[key]`) * May not work as expected with complex data structures or custom comparison logic **Lodash Implementation (`lodash.sortBy`)** The Lodash implementation uses the `_.sortBy()` method: ```javascript _.sortBy(arr, "value"); ``` This implementation is also straightforward and leverages the power of Lodash's internal sorting algorithm. Pros: * Convenient and easy to use * Automatic comparison function generation based on the provided key Cons: * External dependency (Lodash library) * May be slower than native implementation due to overhead of using an external library **Other Considerations** * The benchmark uses a random array of objects with 100,000 elements, which can affect the results. For example, if the data is mostly sorted or has some inherent order, the results may not accurately reflect performance differences. * The benchmark measures executions per second, which is an interesting metric but might not be directly comparable to other benchmarks that measure execution time in seconds or milliseconds. * The Lodash library version used (4.17.5) might be outdated or have security vulnerabilities, affecting the reliability of the results. **Alternatives** If you were to create a similar benchmark, you could consider using alternative implementations, such as: * Using a different sorting algorithm like Quicksort or Mergesort * Implementing your own custom sorting function with different comparison logic * Using a specialized library for sorting data (e.g., React Sortable) * Comparing the performance of other JavaScript libraries that provide sorting functionality When creating a benchmark, it's essential to consider these factors and ensure that your results are representative and accurate.
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?