Skip to content

Commit 76ffd09

Browse files
committed
Add performance timing
1 parent 4d4952a commit 76ffd09

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

SortingAlgorithms/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const getRandomInt = require("./helpers/getRandomInt")
22

3+
const { performance } = require('perf_hooks');
4+
35
bubbleSort = (array) => {
46
for (let i = 0; i < array.length; i++) {
57
for (let j = 0; j < array.length - i - 1; j++) {
@@ -24,7 +26,7 @@ insertionSort = (array) => {
2426
return array
2527
}
2628

27-
quickSortPartition = (array, left = 0, right = array.length - 1) => {
29+
/* quickSortPartition = (array, left = 0, right = array.length - 1) => {
2830
let pivot = array[Math.floor((right + left) / 2)]
2931
let low = left
3032
let high = right
@@ -57,7 +59,7 @@ quickSort = (array, left = 0, right = array.length - 1) => {
5759
5860
return array
5961
}
60-
62+
*/
6163
merge = (arr1, arr2) => {
6264
let sorted = [];
6365

@@ -91,23 +93,27 @@ buildAarray = (n) => {
9193
console.log("BUBBLE---------------------------")
9294
let bubbleArray = buildAarray(20)
9395
console.log(bubbleArray)
96+
const t0 = performance.now();
9497
bubbleArray = bubbleSort(bubbleArray)
98+
const t1 = performance.now();
99+
console.log(`Bubble sort: ${t1 - t0} milliseconds.`);
95100
console.log(bubbleArray)
96101

97-
/* let quickArray = buildAarray(20)
98-
console.log(quickArray)
99-
quickArray = quickSort(quickArray)
100-
console.log(quickArray)
101-
*/
102102
console.log("INSERTION---------------------------")
103103
let insertionArray = buildAarray(20)
104104
console.log(insertionArray)
105-
bubbleArray = insertionSort(insertionArray)
105+
const t2 = performance.now();
106+
insertionArray = insertionSort(insertionArray)
107+
const t3 = performance.now();
108+
console.log(`Insertion sort: ${t3 - t2} milliseconds.`);
106109
console.log(insertionArray)
107110

108111
console.log("MERGE---------------------------")
109112
let mergeArray = buildAarray(20)
110113
console.log(mergeArray)
114+
const t4 = performance.now();
111115
mergeArray = mergeSort(mergeArray)
116+
const t5 = performance.now();
117+
console.log(`Merge sort: ${t5 - t4} milliseconds.`);
112118
console.log(mergeArray)
113119

0 commit comments

Comments
 (0)