<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var fruits = [
{name:"banana", amount: 2},
{name:"apple", amount: 4},
{name:"pineapple", amount: 2},
{name:"mango", amount: 1}
];
_.orderBy(fruits, ['amount'],['asc']);
fruits.sort((first, second) => first.amount - second.amount);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash orderby | |
native sort |
Test name | Executions per second |
---|---|
lodash orderby | 1111068.9 Ops/sec |
native sort | 4334515.0 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
This benchmark compares the performance of two approaches: using Lodash's orderBy
function and implementing a native sorting solution using JavaScript's built-in sorting method.
Options Compared
The two options are:
orderBy
function: This is a utility function from the Lodash library that sorts an array based on one or more specified fields.sort()
method to sort the array, where the comparison function is defined manually.Pros and Cons of Each Approach
orderBy
function:Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. The orderBy
function is one of the many utilities provided by Lodash, allowing developers to easily sort arrays based on specific fields.
Special JS Feature/ Syntax: None mentioned
There are no special JavaScript features or syntax used in this benchmark.
Other Alternatives
If you were to implement a native sorting solution without using Lodash's orderBy
function, you might also consider using other libraries such as:
However, these alternatives would not be directly comparable to Lodash's orderBy
function in terms of simplicity and ease of use.