Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs ES6 sortby
(version: 0)
Comparing performance of:
lodash orderby vs native sort
Created:
6 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:
var fruits = [ {name:"banana", amount: 2}, {name:"apple", amount: 4}, {name:"pineapple", amount: 2}, {name:"mango", amount: 1} ]; sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0); };
Tests:
lodash orderby
_.orderBy(fruits, ['name'],['asc']);
native sort
fruits.concat().sort(sortBy("name"));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash orderby
native sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash orderby
1693079.4 Ops/sec
native sort
5917075.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on the provided JSON benchmark: **Benchmark Definition** The benchmark compares two approaches to sorting an array of objects in ascending order: using Lodash (a popular JavaScript utility library) and the built-in `sort()` method of JavaScript arrays. **Lodash approach** The benchmark defines a custom function `sortBy` that takes a key as an argument. This function returns another function that compares two elements based on the value of the provided key. The comparison is done in ascending order (1 for larger values, -1 for smaller values, and 0 for equal values). The Lodash approach uses this `sortBy` function to sort the `fruits` array. **Native JavaScript approach** The benchmark defines a simple sorting function that takes another function (`sortBy`) as an argument. This function concatenates the original `fruits` array with an empty array, sorts the concatenated array using the provided `sortBy` function, and then discards the sorted result to only keep the modified array. **Pros and Cons** * **Lodash approach** + Pros: - More concise and readable code - Encapsulates sorting logic in a reusable function + Cons: - Requires including Lodash library in the benchmark (which might introduce additional overhead) - Might not be optimized for performance, as it uses a custom sorting algorithm * **Native JavaScript approach** + Pros: - Optimized for performance, as it leverages the built-in `sort()` method - Does not require including any external libraries + Cons: - More verbose and less readable code - Requires creating an intermediate array to sort the original data **Other considerations** * The benchmark uses a relatively small input size (`fruits` array with 4 elements). For larger datasets, the performance difference between these approaches might become more pronounced. * The benchmark does not account for potential edge cases, such as null or undefined values in the `fruits` array. **Library and syntax notes** The Lodash library is a popular JavaScript utility library that provides various functions for tasks like string manipulation, array manipulation, and object manipulation. In this benchmark, it's used to provide a reusable sorting function (`sortBy`). There are no special JS features or syntax mentioned in the benchmark definition or test cases. **Alternatives** If you want to compare different sorting algorithms or approaches without relying on Lodash, you could consider alternative libraries like: * Sort algorithm implementations (e.g., QuickSort, MergeSort) * Custom sorting functions using other data structures (e.g., heap sort) Keep in mind that these alternatives would require more significant changes to the benchmark and might introduce additional complexity. If you prefer not to use external libraries, you could also explore built-in JavaScript sorting algorithms or techniques, such as: * Using a hybrid sorting algorithm (e.g., combining QuickSort with Insertion Sort) * Implementing a custom sorting function using bitwise operations or other low-level optimizations
Related benchmarks:
sortby vs orderby
sortBy performance1
lodash vs ES6 sortby native
lodash vs ES6 sortby native fix
Comments
Confirm delete:
Do you really want to delete benchmark?