Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
SortBy vs Arr.Sort
(version: 0)
Comparing performance of:
Lodash SortBy 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:
function getRandomText(max) { var text = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijz'; for ( var i = 0; i < max; i++ ) { text += characters.charAt(Math.floor(Math.random() * characters.length)); } return text; } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value:getRandomText(10)}); }
Tests:
Lodash SortBy
_.sortBy(arr,"value");
Native Sort
arr.sort((a,b) => a.value - b.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash SortBy
Native Sort
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 what's being tested in this benchmark. **What's being tested:** The benchmark compares the performance of two approaches to sort an array of objects based on a specific property ("value"). **Options compared:** 1. **Native Sort**: This is the built-in sorting algorithm provided by JavaScript, specifically the `sort()` method. 2. **Lodash SortBy**: This uses the `_.sortBy()` function from the Lodash library, which provides a functional programming approach to sorting. **Pros and Cons of each approach:** 1. **Native Sort:** * Pros: + Fast and efficient, especially for small arrays. + Built-in functionality, so no external dependencies are required. * Cons: + Can be slower for large datasets due to the overhead of iterating over the array multiple times. + May not perform well if the dataset is not numerically sorted (e.g., sorting strings lexicographically). 2. **Lodash SortBy:** * Pros: + Provides a more functional programming approach, which can be easier to read and maintain for some developers. + Can be faster than Native Sort for large datasets due to its use of a stable sorting algorithm (e.g., merge sort). * Cons: + Requires the external Lodash library, which adds overhead and potential dependencies. + May not perform as well as Native Sort for very small datasets or datasets with simple sorting requirements. **Library used:** In this benchmark, the `lodash.js` library is being used to provide the `_.sortBy()` function. Lodash is a popular utility library that provides a wide range of functional programming helpers and algorithms, including sorting functions like `_.sortBy()`. The library is included in the HTML preparation code via a CDN link. **Special JS feature or syntax:** This benchmark does not appear to use any special JavaScript features or syntax beyond what's required for the two sorting approaches. However, it's worth noting that the use of Lodash introduces some additional complexity and potential dependencies compared to using only built-in JavaScript functions. **Other alternatives:** If you were considering alternative approaches for sorting arrays in JavaScript, some other options might include: 1. **Array.prototype.sort() with a custom comparator**: This approach uses the `sort()` method with a custom comparator function to specify the sorting key. 2. **Destructuring and spread operators**: Some modern JavaScript engines (e.g., V8) have optimizations for sorting arrays using destructuring and spread operators, which can be faster than traditional sorting algorithms for some use cases. However, these alternatives might not provide the same level of performance or convenience as Native Sort or Lodash's `_.sortBy()` function, depending on the specific requirements and constraints of your application.
Related benchmarks:
Lodash sort vs array.prototype.sortsr23ijo23rf fs a
Lodash sort vs array.prototype.sort with localeCompare
Lodash orderBy vs array.prototype.sort vs vanila orderBy _ smart
Lodash orderBy vs array.prototype.sort vs vanila orderBy _ smart vs QuickSort
Lodash orderBy vs array.prototype.sort vs vanila orderBy _ smart vs QuickSort add slice
Comments
Confirm delete:
Do you really want to delete benchmark?