window.users = [
{ name: 'Ваня', level: '3' },
{ name: 'Саша', level: '1' },
{ name: 'Маша', level: '2' },
{ name: 'Ника', level: '3' },
{ name: 'Вера', level: '1' },
{ name: 'Саша', level: '2' },
{ name: 'Ваня', level: '2' }
];
function swap(items, left, right) {
var temp = [];
if (left !== right) {
temp = items[left];
items[left] = items[right];
items[right] = temp;
}
}
function getItem(item, fieldName) {
if (fieldName) {
item = item[fieldName]
}
return item;
}
function bubbleSort(array, fieldName) {
let { length } = array, items = [], i;
while (length--) {
items[length] = array[length];
}
let swapped = true;
while (swapped) {
swapped = false;
for (i = 1; i < length; i++) {
if (getItem(items[i - 1], fieldName) > getItem(items[i], fieldName)
&& getItem(items[i - 1], fieldName) !== getItem(items[i], fieldName))
{
swap(items, i - 1, i);
swapped = true;
}
}
}
return items;
}
function insertionSort(array, fieldName) {
let { length } = array, items = [];
let i, j, current;
while (length--) {
items[length] = array[length];
}
for (i = 1; i < length; i++) {
current = items[i];
j = i;
while (j > 0 && getItem(items[j - 1], fieldName) > getItem(current, fieldName)) {
items[j] = items[j - 1];
j--;
}
items[j] = current;
}
return items;
}
function selectionSort(array, fieldName) {
let { length } = array, items = [];
let i, j, indexMin;
while (length--) {
items[length] = array[length];
}
for (i = 0; i < length - 1; i++) {
indexMin = i;
for (j = i + 1; j < length; j++) {
if (getItem(items[indexMin], fieldName) > getItem(items[j], fieldName)) {
indexMin = j;
}
}
swap(items, indexMin, i);
}
return items;
}
bubbleSort(users, "level")
insertionSort(users, "level")
selectionSort(users, "level")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Bubble sort | |
Insertion sort | |
Selection sort |
Test name | Executions per second |
---|---|
Bubble sort | 396331.7 Ops/sec |
Insertion sort | 399615.7 Ops/sec |
Selection sort | 403559.6 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition:
The benchmark definition is a JSON object that represents the script to be executed on MeasureThat.net. It contains three functions: bubbleSort
, insertionSort
, and selectionSort
. These functions are designed to sort an array of objects based on a specified field (level
in this case).
Options being compared:
The options being compared are:
These three algorithms are all used to sort arrays, but they have different approaches and performance characteristics.
Pros and cons of each approach:
Library usage:
The swap
function is used in all three sorting algorithms. This function takes two arguments, items
and left
/right
, and swaps the values at these indices. The getItem
function is also used to extract a value from an object based on its field name.
Special JS features or syntax:
There are no special JavaScript features or syntax used in this benchmark.
Benchmark preparation code:
The script preparation code defines an array of user objects with a level
field and three sorting functions. The swap
function is defined to swap two elements, and the getItem
function is defined to extract a value from an object based on its field name.
Individual test cases:
Each individual test case represents one of the sorting algorithms being compared. For example, the first test case runs the bubble sort algorithm using the bubbleSort
function, passing in the users
array and the level
field as arguments.
Other alternatives:
There are other sorting algorithms that could be used instead of these three (bubble, insertion, and selection). Some examples include:
Each of these algorithms has its own strengths and weaknesses, and some may be more suitable for certain use cases than others.