Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash orderBy vs custom
(version: 3)
Comparing performance of:
lodash OrderBy asc vs custom OrderBy asc vs lodash OrderBy desc vs custom OrderBy desc
Created:
4 years ago
by:
Registered User
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:
var things = [ { id: "3", finishedAt: 4 }, { id: "5", finishedAt: null}, { id: "2", finishedAt: 31 }, { id: "6", finishedAt: 12 }, { id: "7", finishedAt: 18 }, { id: "8", finishedAt: 19 }, { id: "9", finishedAt: 16 }, { id: "10", finishedAt: 14 }, { id: "11", finishedAt: 44 }, { id: "12", finishedAt: 53 }, { id: "13", finishedAt: null }, { id: "14", finishedAt: 3 }, { id: "15", finishedAt: 2 }, { id: "16", finishedAt: 32 }, { id: "17", finishedAt: 55 }, { id: "18", finishedAt: 32 }, { id: "19", finishedAt: 11 }, { id: "20", finishedAt: 13 }, { id: "21", finishedAt: 12 }, { id: "22", finishedAt: 21 }, { id: "23", finishedAt: 22 }, { id: "24", finishedAt: 6 }, { id: "25", finishedAt: 2 }, { id: "26", finishedAt: 4 }, { id: "27", finishedAt: 1 }, { id: "1", finishedAt: 1 } ]; console.log(things.concat().sort(orderBy(['finishedAt'], ['desc']))); console.l function sortBy (key, cb) { if (!cb) cb = () => 0 return (a, b) => (a[key] === null && b[key] === null) ? 0 : (a[key] === null) ? 1 : (b[key] === null) ? -1 : (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : cb(a, b)) } function sortByDesc (key, cb) { if (!cb) cb = () => 0 return (b, a) => (a[key] === null && b[key] === null) ? 0 : (a[key] === null) ? 1 : (b[key] === null) ? -1 : (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : cb(b, a)) } function orderBy (keys, orders) { let cb = () => 0 keys.reverse() orders.reverse() for (const [i, key] of keys.entries()) { const order = orders[i] if (order == 'asc') { cb = sortBy(key, cb) } else if (order == 'desc') { cb = sortByDesc(key, cb) } else { throw new Error(`Unsupported order "${order}"`) } } return cb }
Tests:
lodash OrderBy asc
_.orderBy(things,["finishedAt"], ["asc"]);
custom OrderBy asc
things.concat().sort(orderBy(['finishedAt'], ['asc']))
lodash OrderBy desc
_.orderBy(things,["finishedAt"], ["desc"]);
custom OrderBy desc
things.concat().sort(orderBy(['finishedAt'], ['desc']))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash OrderBy asc
custom OrderBy asc
lodash OrderBy desc
custom OrderBy desc
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 dive into the benchmark and explain what's being tested. **What's being tested?** The benchmark is testing the performance of two approaches to sorting an array of objects by a specific key: "lodash orderBy" and a custom implementation using the `sort` method. **Options compared:** 1. **Lodash `orderBy`**: This is a utility function provided by the popular JavaScript library Lodash. It takes an array, a key, and an order (either 'asc' or 'desc') as input. The function returns a sorting function that can be used to sort the array. 2. **Custom implementation**: This is a simple custom implementation of the `sort` method using JavaScript's built-in comparison functions. **Pros and cons:** 1. **Lodash `orderBy`**: * Pros: + Convenient and easy to use + Provides a standardized way of sorting arrays, making it easier for developers to work with * Cons: + Adds an extra dependency on the Lodash library + May not be as efficient as a custom implementation in certain cases 2. **Custom implementation**: * Pros: + No dependencies, just plain old JavaScript + Can be optimized for specific use cases or performance-critical code * Cons: + Requires more manual effort and knowledge of comparison functions **Other considerations:** 1. **Comparison functions**: Both implementations rely on comparison functions to determine the order of elements in the array. The custom implementation uses a combination of null checks, equality comparisons, and conditional statements to handle cases where one or both values are null. 2. **Order handling**: Lodash's `orderBy` function expects the order parameter to be either 'asc' or 'desc'. If an invalid order is provided, it throws an error. The custom implementation handles invalid orders by returning a value of 0 for equal elements and using a large number for other values. **Benchmark results:** The benchmark shows the execution rate (executions per second) for each test case on different browsers and platforms. The custom "OrderBy asc" and "OrderBy desc" cases perform better than the Lodash "OrderBy asc" and "OrderBy desc" cases, indicating that a custom implementation can be more efficient in certain scenarios. Keep in mind that this is just one possible implementation of sorting an array by a specific key, and there may be other approaches or optimizations that could improve performance further.
Related benchmarks:
uniqBy performance
uniqBy performance ttt
order desc with lodash orderBy vs es6 sort method
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?