<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
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)});
}
_.sortBy(arr,"value");
_.orderBy(arr,"value", "asc");
arr.sort((a,b) => a.value - b.value);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.sortBy | |
_.orderBy | |
JS sort |
Test name | Executions per second |
---|---|
_.sortBy | 8.6 Ops/sec |
_.orderBy | 8.9 Ops/sec |
JS sort | 153.1 Ops/sec |
Let's break down the benchmark and analyze what's being tested.
What is being tested?
The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares three approaches for sorting an array of objects with a numerical "value" property: Lodash's _.sortBy
, _.orderBy
, and the built-in JavaScript array method sort
.
Options compared:
_sortBy
: This function takes an array and returns a new sorted array, but unlike the other two options, it doesn't modify the original array. It uses a stable sorting algorithm to maintain the relative order of equal elements._orderBy
: Similar to _.sortBy
, this function sorts the array based on a specific key (in this case, "value") and returns a new sorted array without modifying the original one. However, it allows for ascending or descending ordering by specifying the second argument ("asc" or "-asc").sort
method: This function modifies the original array and sorts it in-place using a stable sorting algorithm.Pros and Cons:
_sortBy
:_orderBy
:sort
._sortBy
.sort
method:Library used:
Lodash is a popular JavaScript utility library that provides a comprehensive set of functions for various tasks, including sorting. The _.sortBy
and _.orderBy
functions are part of Lodash's functional programming utilities.
Special JS feature or syntax:
None mentioned in this benchmark.
Other alternatives:
If you want to implement your own sorting algorithm or use an alternative library, some options include:
Keep in mind that implementing an optimized sorting algorithm requires a good understanding of computer science concepts and performance optimization techniques.