Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lo-Dash _.sortBy() vs Vanilla .sort() vs Vanilla CUSTOM sort method
(version: 3)
Comparing performance of:
Lo-Dash sortBy() vs Vanilla sort() vs Vanilla CUSTOM sort method
Created:
7 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:
window.users = [ { name: 'Ваня', level: '3' }, { name: 'Саша', level: '1' }, { name: 'Маша', level: '2' }, { name: 'Ника', level: '3' }, { name: 'Вера', level: '1' }, { name: 'Саша', level: '2' }, { name: 'Ваня', level: '2' } ]; window.sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0); }; function insertionSort(array, fieldName) { let { length } = array, items = []; let i, j, current; while (length--) { items[length] = array[length]; } for (i = 1; i < length; i++) { current = items[i]; j = i; while (j > 0 && getItem(items[j - 1], fieldName) > getItem(current, fieldName)) { items[j] = items[j - 1]; j--; } items[j] = current; } return items; } function getItem(item, fieldName) { if (fieldName) { item = item[fieldName] } return item; }
Tests:
Lo-Dash sortBy()
_.sortBy(users, 'name');
Vanilla sort()
users.sort(sortBy("name"));
Vanilla CUSTOM sort method
insertionSort(users, "name");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lo-Dash sortBy()
Vanilla sort()
Vanilla CUSTOM sort method
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
Lo-Dash sortBy()
2611395.0 Ops/sec
Vanilla sort()
8529060.0 Ops/sec
Vanilla CUSTOM sort method
58387976.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its various components. **Benchmark Overview** The benchmark, titled "Lo-Dash _.sortBy() vs Vanilla .sort() vs Vanilla CUSTOM sort method", compares the performance of three sorting algorithms: 1. Lo-Dash's `_.sortBy()` function 2. Vanilla JavaScript's built-in `Array.prototype.sort()` function 3. A custom implementation of the insertion sort algorithm **Library: Lodash** The benchmark uses the Lodash library, a popular utility library for JavaScript that provides a wide range of functions for tasks such as string manipulation, array and object manipulation, and more. In this case, it's used to implement the `_sortBy()` function. Lodash's `_sortBy()` function takes an array and returns a new array with the elements sorted according to the provided key. The implementation is based on a stable sort algorithm that preserves the relative order of equal elements. **Options Compared** The benchmark compares three options: 1. **Lo-Dash _.sortBy()**: Uses Lodash's built-in `_sortBy()` function. 2. **Vanilla `.sort()`**: Uses JavaScript's built-in `Array.prototype.sort()` function, which sorts an array in ascending order by default. 3. **Vanilla CUSTOM sort method (Insertion Sort)**: Implements a custom insertion sort algorithm, which is not the most efficient sorting algorithm but is used for demonstration purposes. **Pros and Cons of Each Approach** 1. **Lo-Dash _.sortBy()**: * Pros: Provides a stable and efficient sorting algorithm. * Cons: Requires an additional library (Lodash). 2. **Vanilla `.sort()`**: * Pros: Built-in, widely supported, and efficient for many use cases. * Cons: Not stable by default (preserves relative order of equal elements), and can be slow for large datasets. 3. **Vanilla CUSTOM sort method (Insertion Sort)**: * Pros: Simple to implement, stable, and suitable for small datasets or educational purposes. * Cons: Slow for large datasets due to its O(n^2) time complexity. **Other Considerations** When choosing a sorting algorithm, consider factors such as: * Data size and type * Stability requirements (preserving relative order of equal elements) * Performance demands (real-time applications may require faster sorting algorithms) **Alternative Sorting Algorithms** If you need to sort arrays in JavaScript, here are some alternative algorithms to consider: 1. **Merge Sort**: A divide-and-conquer algorithm with a time complexity of O(n log n). 2. **Quick Sort**: Another divide-and-conquer algorithm with an average time complexity of O(n log n). 3. **Heap Sort**: An in-place sorting algorithm with a time complexity of O(n log n). Keep in mind that each algorithm has its strengths and weaknesses, and the choice ultimately depends on your specific use case and performance requirements.
Related benchmarks:
insert and sort
sortBy performance1
sortBy performance2
lodash vs es6 in sort method
order desc with lodash orderBy vs es6 sort method
Comments
Confirm delete:
Do you really want to delete benchmark?