Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare _.SortBy vs Sort
(version: 0)
lodash sortby equivalent in Javascript
Comparing performance of:
Lodash vs Native
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js</script>
Script Preparation code:
var arr = [ {name: "test", age: 20}, {name: "best", age: 30}, {name: "blah", age: 50}, {name: "js", age: 10}, {name: "style", age: 29}, ];
Tests:
Lodash
_.sortBy(arr, 'age');
Native
const sortBy = function(key){ return (a,b)=> (a[key]>b[key]) ? 1 : (b[key]>a[key]) ? -1 : 0; }; arr.sort(sortBy('age'));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Native
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 is being tested. The benchmark is comparing two approaches to sorting an array of objects in JavaScript: using Lodash's `sortBy` function versus implementing a custom sorting function from scratch. **Lodash's `sortBy` Function** * **Purpose**: Lodash provides a utility function called `sortBy` that allows you to sort arrays of objects based on a specific key. * **Implementation**: The `sortBy` function takes two arguments: the array of objects and the key to sort by. It uses a stable sorting algorithm (usually Timsort or Merge Sort) under the hood, which means it maintains the relative order of equal elements. **Custom Sorting Function** * **Purpose**: This approach implements a simple sorting function from scratch that compares two elements based on their values in the specified key. * **Implementation**: The custom sorting function takes two arguments: `a` and `b`, which are the two elements to compare. It returns a negative value if `a` is less than `b`, zero if they are equal, or a positive value if `a` is greater than `b`. This is known as a "strict" comparison. **Pros and Cons** * **Lodash's `sortBy` Function** * Pros: * Faster execution times ( thanks to the optimized sorting algorithm) * More concise code * Less chance of errors due to its use of a stable sorting algorithm * Cons: * External dependency on Lodash (adds overhead for load time and potential compatibility issues) * Less control over the sorting process * **Custom Sorting Function** * Pros: * No external dependencies or load times * More control over the sorting process * Potential for better performance due to a simpler implementation * Cons: * Slower execution times (due to the need for explicit comparisons) * Higher chance of errors if not implemented correctly **Other Considerations** * **Stability**: Both approaches are stable, meaning that equal elements will maintain their relative order. * **Performance**: The Lodash approach is generally faster due to its optimized sorting algorithm. However, the custom approach may outperform it in certain cases where a simple comparison is sufficient. **Library and Special JS Feature** There are no specific libraries or special JavaScript features used in this benchmark beyond what's standard for both approaches (i.e., array sorting). **Alternative Approaches** Some alternative approaches to sorting arrays of objects include: * Using `Array.prototype.sort()` with a custom compare function * Using a library like Moment.js to sort dates * Using a library like jQuery to sort DOM elements However, the question is specifically asking about comparing `_.sortBy` from Lodash and native sorting functions. In summary, this benchmark compares two approaches to sorting arrays of objects in JavaScript: using Lodash's `sortBy` function versus implementing a custom sorting function from scratch.
Related benchmarks:
_.SortBy vs Sort
compare _.SortBy vs Sort 1234
sortby vs orderby
lodash vs es6 in sort method
Comments
Confirm delete:
Do you really want to delete benchmark?