Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash_4.17.21.sortBy vs .sort vs custom
(version: 0)
Comparing performance of:
Lo-Dash sortBy() vs Vanilla sort() vs Vanilla CUSTOM sort method
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lo-Dash sortBy()
529300.1 Ops/sec
Vanilla sort()
1780777.0 Ops/sec
Vanilla CUSTOM sort method
4580862.0 Ops/sec
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 options, pros and cons of each approach, and other considerations. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The test compares three sorting algorithms: 1. `_.sortBy` from Lodash library 2. Vanilla JavaScript's built-in `sort()` method 3. A custom implementation using insertion sort **What's being tested** The benchmark tests the performance of each sorting algorithm on an array of objects, specifically when sorting by a "name" field. **Options compared** 1. **Lodash's `_sortBy`**: A functional programming approach that takes a key function as an argument and returns a sorted array. 2. **Vanilla JavaScript's `sort()`**: A built-in method that sorts the array in-place using a compare function. 3. **Custom insertion sort**: An implementation of the insertion sort algorithm, which is a simple sorting algorithm with a time complexity of O(n^2). **Pros and Cons** 1. **Lodash's `_sortBy`**: * Pros: Easy to use, concise code, and well-maintained library. * Cons: May have a higher memory overhead due to the creation of an intermediate array. 2. **Vanilla JavaScript's `sort()`**: * Pros: Built-in method, easy to understand, and fast for large datasets. * Cons: Requires a compare function, which can be error-prone if not implemented correctly. 3. **Custom insertion sort**: * Pros: Simple to implement, efficient for small datasets, and can be optimized for specific use cases. * Cons: Higher time complexity (O(n^2)) compared to other algorithms, making it less suitable for large datasets. **Other considerations** * The benchmark uses Chrome 124 as the test browser, which may not represent the entire user base. * The test executes per second metric might not accurately reflect real-world performance, as it's a simplified measure that doesn't account for factors like garbage collection and other overheads. **Library used - Lodash** Lodash is a popular JavaScript library providing various utility functions, including sorting. `_sortBy` is a part of the Lodash collection and allows for easy and expressive sorting of arrays using a key function. **Special JS feature/Syntax** There are no special JavaScript features or syntaxes mentioned in this benchmark that would require any additional explanations. **Alternatives** Other sorting algorithms that could be included in this benchmark include: * Quicksort * Merge sort * Heap sort * Radix sort (for string-based data) However, these alternatives might not provide significant enough performance differences to justify the added complexity and might not be as easily comparable to Lodash's `_sortBy` and vanilla JavaScript's `sort()` methods.
Related benchmarks:
sortBy performance1
lodash vs es6 in sort method
lodash uniqBy vs custom uniqBy
order desc with lodash orderBy vs es6 sort method
native sort of objects by linus
Comments
Confirm delete:
Do you really want to delete benchmark?