function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var arr = [];
for(var i = 0; i < 100000; i++){
arr.push([-1,getRandomInt(100)]);
}
arr.map(r => r[1]).sort()
arr.sort((a,b) => a[1]-b[1]).map(r => r[1])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map & sort | |
sort & map |
Test name | Executions per second |
---|---|
map & sort | 66.5 Ops/sec |
sort & map | 188.2 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition
The benchmark is designed to compare two approaches to mapping and sorting an array of arrays:
arr.map(r => r[1]).sort()
arr.sort((a,b) => a[1]-b[1]).map(r => r[1])
These two approaches are essentially the same, but in different orders.
Options Compared
The benchmark is comparing the performance of these two approaches:
Both approaches have their own pros and cons:
map & sort
:map & sort
:sort & map
:sort & map
:Library
There is no explicit library mentioned in the benchmark definition or test cases. However, it's likely that some built-in JavaScript functions are being used, such as map()
, sort()
, and Array.prototype.push()
.
Special JS Feature/Syntax
No special JavaScript features or syntax are being tested in this benchmark.
Other Considerations
When writing benchmarks like this, consider the following:
Alternatives
If you're interested in exploring alternative approaches or optimization techniques, consider the following:
sort()
function, try implementing a custom sorting algorithm that takes advantage of specific properties of your data.