var a=Array.from({length:100},()=>Math.random())
function insertionSort(arr){
//Start from the second element.
for(let i = 1; i < arr.length;i++){
//Go through the elements behind it.
for(let j = i - 1; j > -1; j--){
//value comparison using ascending order.
if(arr[j + 1] < arr[j]){
//swap
[arr[j+1],arr[j]] = [arr[j],arr[j + 1]];
}
}
};
return arr;
}
function selectionSort(arr) {
let min;
//start passes.
for (let i = 0; i < arr.length; i++) {
//index of the smallest element to be the ith element.
min = i;
//Check through the rest of the array for a lesser element
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
//compare the indexes
if (min !== i) {
//swap
[arr[i], arr[min]] = [arr[min], arr[i]];
}
}
return arr;
}
var b = a.slice();
b.sort();
var b = a.slice();
insertionSort(b)
var b = a.slice();
selectionSort(b)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
sort | |
insertion sort | |
select sort |
Test name | Executions per second |
---|---|
sort | 23000.0 Ops/sec |
insertion sort | 3864.2 Ops/sec |
select sort | 22060.8 Ops/sec |
The provided JSON represents a benchmark test that compares the performance of three sorting algorithms: Native JavaScript Sort, Insertion Sort, and Selection Sort.
What is tested?
sort()
method, which likely uses a hybrid sorting algorithm (e.g., Timsort).Options compared
The benchmark compares the performance of these three algorithms on an array of 100 random numbers. The goal is to identify which algorithm is the fastest, most efficient, and suitable for specific use cases.
Pros and Cons of each approach:
Other considerations:
Library used:
None explicitly mentioned in the provided JSON. However, it is likely that the native JavaScript sort()
method uses a library or implementation under the hood (e.g., Timsort).
Special JS feature/syntax:
The benchmark does not use any special JavaScript features or syntax that would affect its execution. The scripts are written in vanilla JavaScript and do not rely on any modern ES6+ features.
Alternatives:
Other sorting algorithms that could be tested include:
These algorithms might offer better performance or efficiency for specific use cases, such as: