<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");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.sortBy | |
_.orderBy |
Test name | Executions per second |
---|---|
_.sortBy | 12.8 Ops/sec |
_.orderBy | 11.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Definition JSON
The benchmark definition defines two individual test cases: _.sortBy
and _.orderBy
, both using the Lodash library. The test case for _.sortBy
involves sorting an array of objects in ascending order based on a specific value (in this case, "value"). Similarly, the test case for _.orderBy
also sorts the same array, but in descending order.
Lodash Library
The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks such as:
In this benchmark, Lodash is used to sort arrays. The _.sortBy
and _.orderBy
functions are two specific functions provided by Lodash that can be used to sort arrays in different ways.
Comparison Options
The two comparison options being compared here are:
Pros and Cons of Each Approach
Lodash sortBy
Pros:
Cons:
_.orderBy
.Lodash orderBy
Pros:
Cons:
Other Considerations
When choosing between these two options, consider the following factors:
_.orderBy
.Special JS Feature/Syntax
There is no special JavaScript feature or syntax being used in this benchmark.
Alternatives
If you're looking for alternative libraries or implementations, consider:
Keep in mind that these alternatives might have different trade-offs in terms of performance, complexity, and ease of use compared to Lodash's implementation.