<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = []
_.sortBy(arr)
_.orderBy(arr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.sortBy(arr) | |
_.orderBy(arr) |
Test name | Executions per second |
---|---|
_.sortBy(arr) | 1024916.4 Ops/sec |
_.orderBy(arr) | 1261111.4 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two functions from the Lodash library: _.sortBy
and _.orderBy
. Both functions are designed to sort an array of elements in a specific order.
What is being compared?
The benchmark tests how fast each function can execute on the same input data. Specifically, it measures the time it takes for each function to sort an empty array (var arr = []
) using the Lodash library.
Options being compared:
There are two options being compared:
_.sortBy(arr)
: This function uses a stable sorting algorithm that maintains the relative order of equal elements. The implementation is likely to be slower due to its stability guarantee._.orderBy(arr)
: This function uses an unstable sorting algorithm that does not maintain the relative order of equal elements. The implementation might be faster, but it's less predictable in terms of the final sorted order.Pros and Cons:
Lodash Library
The Lodash library is a popular utility belt for JavaScript developers. It provides a wide range of functions for tasks like array manipulation, object iteration, and more. In this benchmark, two functions from Lodash are being compared: _.sortBy
and _.orderBy
.
Other Considerations
When choosing between these two options, consider the following:
_.sortBy(arr)
._.orderBy(arr)
. However, be aware of the potential for unpredictable results.Alternative approaches
If you're not using Lodash or want to explore alternative approaches:
Array.prototype.sort()
method, which is also implemented in JavaScript.Keep in mind that these alternatives might not provide the same level of stability guarantees as _.sortBy(arr)
and may have different performance characteristics.