<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i + 1));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var words = ["aaa", "bbb", "ccc", "AAA", "BBB", "CCC", "123", "234", "345", "aaa", "bbb", "ccc", "AAA", "BBB", "CCC", "123", "234", "345", "aaa", "bbb", "ccc", "AAA", "BBB", "CCC", "123", "234", "345", "aaa", "bbb", "ccc", "AAA", "BBB", "CCC", "123", "234", "345", "가나다", "나다라", "마바사"];
var words1 = [];
var n = [];
for (i = 0; i < 4; i++) words.push(words);
for (i = 0; i < words.length; i++) n.push(i);
var ranNums = shuffle(n);
for (i = 0; i < words.length; i++) words1.push(words[ranNums[i]]);
console.log('ORIGINAL', words1);
words1.sort((a,b) => a.localeCompare(b));
_.orderBy(words1,model => model,'asc');
words1.sort((a,b) => {
if (a.toLowerCase() < b.toLowerCase()) {
return -1;
}
if (a.toLowerCase() > b.toLowerCase()) {
return 1;
}
return 0;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
localeCompare | |
lodash order | |
sort comparator |
Test name | Executions per second |
---|---|
localeCompare | 188041.2 Ops/sec |
lodash order | 61582.6 Ops/sec |
sort comparator | 77867.5 Ops/sec |
Let's break down the provided benchmark and explain what is tested, compared options, pros/cons of those approaches, and other considerations.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The test compares the performance of three different sorting algorithms:
localeCompare
sortorderBy
functionThe test uses an array of 57,200 words and creates two arrays: words1
(the original array) and n
(a shuffled version of the indices). The test case is prepared using JavaScript code that shuffles the indices and then combines them with the corresponding words.
Options Compared
Here's a brief overview of each option compared:
localeCompare
sortThe localeCompare
method compares two strings by sorting them in a locale-dependent manner. This means that the comparison is influenced by the locale settings of the user's system, which can affect the ordering of certain characters.
Pros: Fast and efficient for most use cases, as it leverages the browser's built-in sorting algorithm.
Cons:
orderBy
functionThe Lodash orderBy
function is a utility function that sorts an array based on a specified key and sorting order (ascending or descending). In this test, it's used with a model function that extracts the string value from each object in the array.
Pros:
localeCompare
sort for very large datasets, as it avoids locale-dependent comparisons.Cons:
The custom sorting approach uses a compare function that compares two strings by converting them to lowercase and comparing the results. This approach allows for fine-grained control over the comparison process.
Pros:
localeCompare
sort or Lodash's orderBy
function if the comparison logic is optimized.Cons:
Other Considerations
Alternatives
Some alternatives to consider when implementing similar benchmarks include:
Array.prototype.sort()
with a custom compare function instead of localeCompare
.I hope this explanation helps you understand the provided benchmark and its comparisons!