<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = [
{ 'name': 'lim', 'age': 26 },
{ 'name': 'kim', 'age': 28 },
{ 'name': 'choi', 'age': 32 },
{ 'name': 'park', 'age': 21 }
];
var result = _.orderBy(array, ['age'], ['desc']);
var result = array.sort((prev, cur) => cur.age - prev.age);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash sort method | |
es6 sort method |
Test name | Executions per second |
---|---|
lodash sort method | 1177859.5 Ops/sec |
es6 sort method | 4750865.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The provided benchmark compares two approaches to sorting an array in descending order: Lodash's orderBy
function and the built-in ECMAScript 6 (ES6) sort
method. The benchmark is designed to measure the performance difference between these two methods, which are commonly used for data processing and manipulation.
Options Compared
Two options are compared:
orderBy
function: This is a utility function that takes an array and specifies one or more keys to sort on, as well as the direction of sorting (ascending or descending). In this benchmark, it is used with only one key ('age'
) in ascending order.sort
method: This is a built-in method for sorting arrays in JavaScript. It takes a comparison function as an argument, which defines the sorting logic.Pros and Cons of Each Approach
orderBy
function:sort
method:orderBy
function, which supports multiple keys and directions.Library Used
The benchmark uses the Lodash library, which is a popular utility library for JavaScript. In this case, it is used specifically for its orderBy
function, which provides a simple way to sort arrays based on one or more keys.
Special JS Feature/ Syntax
There are no special JavaScript features or syntax used in this benchmark. The only notable aspect is the use of the ES6 sort
method, which is a standard feature in modern JavaScript environments.
Other Alternatives
Other alternatives for sorting arrays include:
slice()
and then sorting it using the sort()
method.map()
) and then checking if the resulting array is sorted correctly (using every()
).These alternatives may offer better performance for specific use cases or large datasets, but they often come with additional overhead and complexity.