Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.sortBy vs native multiple property sort
(version: 0)
Comparing performance of:
Sort by multiple properties vs Lodash sortBy
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
Script Preparation code:
var users = [ { 'user': 'joey', 'age': 29 }, { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'ross', 'age': 25 }, { 'user': 'ross', 'age': 48 }, { 'user': 'chandler', 'age': 39 }, { 'user': 'chandler', 'age': 12 }, { 'user': 'chandler', 'age': 41 } ]
Tests:
Sort by multiple properties
const by = (fields) => (a, b) => fields.map(o => { let dir = 1; if (o[0] === '-') { dir = -1; o=o.substring(1); } return a[o] > b[o] ? dir : a[o] < b[o] ? -(dir) : 0; }).reduce((p, n) => p ? p : n, 0); let sorted = users.sort(by(['user', 'age'])); console.log(sorted);
Lodash sortBy
let sorted = _.sortBy(users, ['user', 'age']); console.log(sorted);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Sort by multiple properties
Lodash sortBy
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's being tested, compared, and considered. **What is being tested?** The benchmark compares two approaches to sorting an array of objects with multiple properties: 1. A custom implementation using a reduce function, which sorts the array based on multiple properties. 2. The `sortBy` function from the Lodash library, which sorts an array of objects based on one or more keys. **Options compared:** The benchmark compares two options for sorting: 1. Custom implementation: uses a reduce function to iterate over each property and compare values. This approach requires manual iteration over each property. 2. Lodash `sortBy`: uses the Lodash library's built-in sorting functionality, which is optimized for performance and handles multiple properties efficiently. **Pros and Cons of each approach:** 1. Custom implementation: * Pros: Simple to implement, no dependencies on external libraries. * Cons: Requires manual iteration over each property, may be slower due to the overhead of the reduce function. 2. Lodash `sortBy`: * Pros: Optimized for performance, handles multiple properties efficiently, and doesn't require manual iteration. * Cons: Dependencies on the Lodash library, which may introduce additional overhead. **Library used:** The benchmark uses the Lodash library, specifically the `sortBy` function. Lodash is a popular utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. **Special JS features or syntax:** This benchmark doesn't use any special JavaScript features or syntax beyond standard ES6 syntax. **Other considerations:** * The benchmark uses the `console.log` function to display the sorted arrays, which may affect performance. * The benchmark runs on a Mac OS X 10.15.7 system with Chrome 115 browser, which may introduce platform-specific variations in performance. * The benchmark only compares two options for sorting, so additional comparisons or alternatives would require more test cases. **Alternative approaches:** Other approaches to sorting arrays of objects include: 1. Using the `Array.prototype.sort()` method, which is a built-in JavaScript function that sorts arrays based on one or more properties. 2. Using a third-party library like Moment.js for date-based sorting. 3. Implementing a custom sorting algorithm using algorithms like mergesort or quicksort. Keep in mind that each approach has its own trade-offs and may be better suited for specific use cases or performance requirements.
Related benchmarks:
native sort of objects by localeCompare vs lodash _.orderBy
native sort of objects by localeCompare vs lodash _.sortBy
native sort of objects by linus
native sort of objects by localeCompare vs lodash _.sortBy []
Comments
Confirm delete:
Do you really want to delete benchmark?