Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs native sortBy (ascending only)
(version: 0)
Comparing performance of:
Native sortBy 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:
Native sortBy
const by = (fields) => (a, b) => fields.map(o => { return a[o] > b[o] ? 1 : a[o] < b[o] ? -1 : 0; }).reduce((p, n) => p ? p : n, 0); users.sort(by(['user', 'age']));
Lodash sortBy
_.sortBy(users, ['user', 'age']);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native sortBy
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):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two approaches for sorting an array of objects: native JavaScript's `sort()` method versus Lodash's `sortBy()` function. The benchmark measures which approach is faster in ascending order. **Options Compared** Two options are compared: 1. **Native JavaScript's sort() method**: This is the built-in Array method that sorts an array of elements in place, according to a provided comparison function. 2. **Lodash's sortBy() function**: Lodash is a utility library for JavaScript that provides functional programming helpers, including `sortBy()`. **Pros and Cons** ### Native JavaScript's sort() method Pros: * Fast and efficient * Built-in method, so it's likely to be well-optimized by the browser * Can handle large datasets efficiently Cons: * Requires a comparison function, which can be complex or time-consuming to implement * May not work correctly for certain data types (e.g., strings with special characters) ### Lodash's sortBy() function Pros: * Convenient and easy to use * Provides a simple and efficient way to sort arrays without requiring custom implementation * Can handle large datasets efficiently Cons: * External dependency on the Lodash library, which may add overhead * May not be as optimized as native JavaScript code for certain scenarios **Library: Lodash** Lodash is a popular utility library that provides a wide range of functional programming helpers. `sortBy()` is one of its most useful functions, allowing users to easily sort arrays without requiring custom implementation. In the benchmark, Lodash's `sortBy()` function is used to sort an array of objects in ascending order based on two fields (`user` and `age`). The comparison function is not explicitly provided, as it defaults to a stable sorting algorithm that compares each field in turn. **Special JS Feature/Syntax** None mentioned in the benchmark definition. However, it's worth noting that Lodash uses a specific syntax for its functions (e.g., `_` prefix), which is not part of standard JavaScript. **Alternatives** Other alternatives for sorting arrays include: * Other library-based functions (e.g., Moment.js for date-based sorting) * Custom implementation using native JavaScript methods (e.g., `Array.prototype.sort()`) * Third-party libraries or frameworks that provide built-in sorting functionality In conclusion, the benchmark provides a useful comparison between native JavaScript's `sort()` method and Lodash's `sortBy()` function. The choice of which approach to use depends on factors like performance requirements, ease of implementation, and external dependencies.
Related benchmarks:
lodash vs es6 in sort method
native sort of objects by localeCompare vs lodash _.sortBy
order desc with lodash orderBy vs es6 sort method
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?