<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 = _.sortBy(array, 'age');
var result = array.sort((prev, cur) => prev.age - cur.age);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash sort method | |
es6 sort method |
Test name | Executions per second |
---|---|
lodash sort method | 522767.9 Ops/sec |
es6 sort method | 2101934.5 Ops/sec |
Let's break down the benchmark and explain what is being tested.
Overview
The benchmark compares the performance of two approaches for sorting an array of objects: the _.sortBy
function from the Lodash library and the built-in sort
method with a custom comparison function in ECMAScript 6 (ES6).
Options Compared
There are two options compared:
Pros and Cons
Library and Purpose
The Lodash library is a popular utility function collection for JavaScript. The _.sortBy
function is one of its many functions that provides a convenient way to sort arrays using various sorting algorithms.
Special JS Feature or Syntax
There are no special JS features or syntax used in this benchmark. The tests only use standard JavaScript and the sort
method with a custom comparison function.
Other Considerations
When deciding between these two approaches, consider the following:
sort
method can be sensitive to edge cases, such as NaN (Not a Number) values or objects with non-standard properties.Alternatives
Other alternatives for sorting arrays in JavaScript include:
Array.prototype.sort()
method without a custom comparison function (which is similar to the ES6 sort method but may not handle complex cases correctly).In general, when choosing a sorting approach, consider the size and complexity of your data, the required level of stability, and the performance requirements of your application.