Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sortBy vs orderBy [corrected]
(version: 0)
Lodash sortBy vs orderBy
Comparing performance of:
_.sortBy vs _.orderBy
Created:
7 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 < 100000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.sortBy
_.sortBy(arr,"value");
_.orderBy
_.orderBy(arr,"value");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sortBy
_.orderBy
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing different approaches to achieve the same result. **Script Preparation Code** The script preparation code generates an array of 100,000 objects with a random "value" property. This creates a large dataset for benchmarking. ```javascript function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for (var i = 0; i < 100000; i++) { arr.push({ value: getRandomInt(100) }); } ``` **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library version 4.17.5. ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` This indicates that the benchmark uses Lodash as a dependency. **Benchmark Definition** The benchmark definition consists of two individual test cases: 1. `_.sortBy(arr, "value");` 2. `_.orderBy(arr, "value");` These two functions are part of the Lodash library and are used to sort the array in different ways. * `_.sortBy(arr, "value")` sorts the array based on the value property of each object. * `_.orderBy(arr, "value")` also sorts the array based on the value property, but it uses a different algorithm that may be faster for large datasets. **Options Compared** The two options being compared are: 1. `_.sortBy(arr, "value")`: Sorts the array using the `sortBy` function. 2. `_.orderBy(arr, "value")`: Sorts the array using the `orderBy` function. **Pros and Cons of Different Approaches** * **_.sortBy**: This approach sorts the array based on a stable sort (i.e., it preserves the order of equal elements). However, for large datasets, this can be slower due to the overhead of the sorting algorithm. * **_.orderBy**: This approach also sorts the array but uses a more efficient algorithm that is optimized for large datasets. However, it may not preserve the stability of the sort. **Other Considerations** When choosing between `_.sortBy` and `_.orderBy`, consider the following factors: * **Data Size**: If you're working with very large datasets, `_.orderBy` might be a better choice due to its optimized algorithm. * **Stability**: If you need to preserve the order of equal elements, `_.sortBy` is a safer choice. * **Performance**: Measure the execution time for both functions on your specific dataset and choose the one that performs better. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. The code focuses on demonstrating the difference between two Lodash functions, `_.sortBy` and `_.orderBy`. **Alternatives** If you're looking for alternatives to Lodash or want to compare other sorting libraries, consider the following options: * **Array.prototype.sort()**: Built-in JavaScript method that sorts an array in place. * **lodash-es** (ES6+ compatible): A modern version of Lodash that is optimized for ES6+ environments. * **Ramda**: A functional programming library that includes a sorting function, `R.sort()`. Keep in mind that these alternatives might have different performance characteristics or require specific versions of JavaScript to work.
Related benchmarks:
Lodash orderBy() vs array.prototype.sort
lodash sortBy vs native sortBy 4.17.21
Lodash sortBy vs orderBy performance
Lodash orderBy vs array.prototype.sort fork
Lodash orderBy (fn) vs array.prototype.sort small array
Comments
Confirm delete:
Do you really want to delete benchmark?