<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var test = [{"value":"97535676-acbb-41f4-b8ea-91e8e5e3fc0d","label":"Overview"},{"value":"f1e5845d-b5a4-4725-9813-f601eaed6826","label":"test 1"},{"value":"056d7e5a-b947-4c10-8b38-13416deebe4a","label":"Announcements"},{"value":"18149d83-8191-4c60-afca-d976f6f63ca5","label":"Browse buttons"},{"value":"20b145fa-14ae-457c-9fec-04fdd48ddba2","label":"ztest"},{"value":"00142552-9bf6-49a8-829d-152d8448ac5f","label":"Watson News"},{"value":"578695c6-4e4c-4c8d-ba8a-2ede7b6abd19","label":"Filtered"},{"value":"6b6fef15-a13e-423b-875d-7700737dccf4","label":"Curated"}]
_.sortBy(test, [
name => {
const priority = ['overview'];
if (_.includes(priority, name)) {
return _.indexOf(priority, name);
} else {
return name.length;
}
},
name => name,
]);
test.sort(function(a, b) {
const valueA = a.label.toUpperCase();
const valueB = b.label.toUpperCase();
if (valueB === 'OVERVIEW') {
return 1;
} else if (valueA === 'OVERVIEW') {
return -1;
}
return valueA < valueB ? -1 : valueA > valueB ? 1 : 0;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
JS |
Test name | Executions per second |
---|---|
lodash | 130392.3 Ops/sec |
JS | 1028969.4 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and the pros/cons of each approach.
Benchmark Overview
The benchmark compares the performance of two approaches for sorting an array of objects:
sortBy
functionTest Cases
There are two test cases:
sortBy
function to sort the test
array based on two criteria:name => _.includes(priority, name)
: If the name
is in the priority
array (which contains only "overview"), returns the index of the name
in the priority
array. Otherwise, returns the length of the name
.name => name
: Sorts by the label
property of each object.test
array based on two criteria:a.label.toUpperCase()
and b.label.toUpperCase()
: Compare the uppercase labels of each object. If one label is "OVERVIEW", returns -1 if it comes before another "OVERVIEW" label, 1 if it comes after, or 0 if they are equal.Library and Purpose
Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string manipulation, and more. The sortBy
function in Lodash allows you to sort an array of objects based on one or multiple criteria.
Special JS Feature/Syntax
None mentioned in the benchmark definition.
Comparison of Approaches
Here's a brief analysis of each approach:
sortBy
: This approach uses a stable sorting algorithm (Timsort, which is also used by JavaScript's built-in sort function) to sort the array. The use of Lodash provides a convenient and expressive way to specify the sorting criteria.Other Alternatives
If you don't want to use Lodash, you could consider using other JavaScript libraries or frameworks that provide similar sorting functionality, such as:
orderBy
pipe for data binding and sortingsort
function in the lodash
moduleAlternatively, if you prefer not to use any external dependencies, you could implement your own sorting algorithm using JavaScript's built-in functions, such as Array.prototype.sort()
.
I hope this explanation helps!