Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sortBy vs native sortBy 3
(version: 0)
Comparing performance of:
_.sortBy vs sortByKey
Created:
5 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:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 500; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.sortBy
_.sortBy(arr,"value");
sortByKey
const sortByKey = (array, key) => arr.sort((a,b) => (a[key] > b[key]) ? 1 : (a[key] < b[key]) ? -1 : 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sortBy
sortByKey
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 break down the provided benchmark JSON and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Description** The benchmark measures the performance difference between two approaches to sorting an array: 1. **`_.sortBy(arr,"value")`:** This is a method call on the Lodash library (specifically, version 4.17.5), which implements a stable sort function using a comparison function. 2. **`const sortByKey = (array, key) => arr.sort((a,b) => (a[key] > b[key]) ? 1 : (a[key] < b[key]) ? -1 : 0);`:** This is an anonymous function that sorts the array using the `sort()` method and a comparison function. **Comparison of Sorting Algorithms** The benchmark compares two sorting algorithms: * **Lodash's `_sortBy`**: This uses a stable sort algorithm, which ensures that equal elements maintain their original order. * **Native JavaScript's `sort()` with a custom comparison function**: This uses the built-in `sort()` method, which sorts arrays in ascending order by default. By providing a custom comparison function, you can sort the array based on a specific key. **Pros and Cons of Each Approach** **Lodash's `_sortBy`** Pros: * Stable sorting algorithm ensures equal elements maintain their original order. * Implemented in JavaScript, so no platform-specific dependencies are required. * Lodash is widely used and well-maintained. Cons: * May be slower than native JavaScript's `sort()` due to the overhead of a library function. * Additional dependency on Lodash needs to be included. **Native JavaScript's `sort()` with a custom comparison function** Pros: * Fastest implementation, as it leverages the optimized built-in `sort()` method. * No additional dependencies or libraries needed. Cons: * Unstable sorting algorithm (equal elements may swap order). * Custom comparison function can make code more difficult to read and maintain. **Library Used: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like string manipulation, array processing, and more. The `_sortBy` method is part of the `lodash.sortBy` module, which implements a stable sorting algorithm. **Special JS Feature or Syntax: None mentioned** There are no special JavaScript features or syntaxes being used in this benchmark that require explanation. **Alternatives to Lodash's `_sortBy`** If you don't want to use an external library like Lodash, you can implement your own sorting algorithms using native JavaScript functions. Some popular alternatives include: * **Quicksort**: A fast and efficient sorting algorithm with an average time complexity of O(n log n). * **Mergesort**: Another stable sorting algorithm with a time complexity of O(n log n). However, these alternatives may not be as widely used or well-maintained as Lodash's `_sortBy`. **Alternatives to Native JavaScript's `sort()`** If you don't want to use the built-in `sort()` method, you can implement your own sorting algorithm using native JavaScript functions. Some popular alternatives include: * **Bubble sort**: A simple and stable sorting algorithm with a time complexity of O(n^2). * **Insertion sort**: Another simple and efficient sorting algorithm with a time complexity of O(n^2). However, these alternatives may not be as fast or efficient as the built-in `sort()` method.
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
Sort lodash vs native
lodash sortBy vs native sortBy 4.17.21
Lodash sortBy vs orderBy performance
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?