const list = [20, 10, 50, 33, 1, 5, 90, 45, 39, 77, 11];
function built_in_sort(arr) {
return arr.sort((a, b) => a - b);
}
//Insertion sort
function insertion_sort(ary) {
for (var i = 1, l = ary.length; i < l; i++) {
var value = ary[i];
for (var j = i - 1; j >= 0; j--) {
if (ary[j] <= value)
break;
ary[j + 1] = ary[j];
}
ary[j + 1] = value;
}
return ary;
}
built_in_sort([20, 10, 50, 33, 1, 5, 90, 45, 39, 77, 11])
insertion_sort([20, 10, 50, 33, 1, 5, 90, 45, 39, 77, 11])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Built in sort | |
Insertion sort |
Test name | Executions per second |
---|---|
Built in sort | 3713366.5 Ops/sec |
Insertion sort | 14889111.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is comparing two sorting algorithms: built-in JavaScript sort and insertion sort. The script preparation code defines both algorithms:
built_in_sort
: This function uses the built-in JavaScript sort()
method with a custom comparison function (a, b) => a - b
.insertion_sort
: This function implements a standard insertion sort algorithm.Options compared
The benchmark is comparing two options:
sort()
method with a custom comparison function.Pros and Cons of each approach
Library/dependency
The sort()
method in JavaScript uses the compare()
function from the Array.prototype
object. This is a standard library function that performs a comparison between two elements and returns a value indicating their order.
Special JS feature/syntax
There is no special JavaScript feature or syntax being used in this benchmark. The focus is on comparing two existing sorting algorithms.
Other alternatives
If you want to include more sorting algorithms in your benchmark, here are some alternatives:
These alternatives can provide a more comprehensive understanding of different sorting algorithms and their performance characteristics.
Benchmark preparation code
The provided JSON includes the script preparation code, which defines both the built-in JavaScript sort()
method and the insertion sort algorithm. This allows the benchmark to execute these algorithms with identical input data.
Individual test cases
Each individual test case compares a specific scenario:
sort()
method is executed on an array of 11 elements.By comparing these two algorithms, we can understand their performance characteristics and determine which one might be more suitable for specific use cases or scenarios.