1 const Benchmark
= require('benny')
2 const { generateRandomInteger
} = require('../benchmarks-utils')
4 function generateRandomTasksMap (
6 maxNumberOfTasksPerWorker
= 10
9 for (let i
= 0; i
< numberOfWorkers
; i
++) {
10 const task
= [i
, generateRandomInteger(maxNumberOfTasksPerWorker
)]
13 return new Map(tasksArray
)
16 const tasksMap
= generateRandomTasksMap(60, 20)
18 function loopSelect (tasksMap
) {
19 let minValue
= Infinity
21 for (const [key
, value
] of tasksMap
) {
24 } else if (value
< minValue
) {
29 return [minKey
, minValue
]
32 function arraySortSelect (tasksMap
) {
33 const tasksArray
= Array
.from(tasksMap
)
34 return tasksArray
.sort((a
, b
) => {
37 } else if (a
[1] > b
[1]) {
44 const defaultComparator
= (a
, b
) => {
48 const defaultPivotIndexSelect
= (leftIndex
, rightIndex
) => {
49 return leftIndex
+ Math
.floor((rightIndex
- leftIndex
) / 2)
52 const randomPivotIndexSelect
= (leftIndex
, rightIndex
) => {
53 return generateRandomInteger(rightIndex
, leftIndex
)
56 function swap (array
, index1
, index2
) {
57 const tmp
= array
[index1
]
58 array
[index1
] = array
[index2
]
67 compare
= defaultComparator
69 const pivotValue
= array
[pivotIndex
]
70 swap(array
, pivotIndex
, rightIndex
)
71 let storeIndex
= leftIndex
72 for (let i
= leftIndex
; i
< rightIndex
; i
++) {
73 if (compare(array
[i
], pivotValue
)) {
74 swap(array
, storeIndex
, i
)
78 swap(array
, rightIndex
, storeIndex
)
87 compare
= defaultComparator
,
88 pivotIndexSelect
= defaultPivotIndexSelect
91 if (leftIndex
=== rightIndex
) return array
[leftIndex
]
92 let pivotIndex
= pivotIndexSelect(leftIndex
, rightIndex
)
93 pivotIndex
= partition(array
, leftIndex
, rightIndex
, pivotIndex
, compare
)
94 if (k
=== pivotIndex
) {
96 } else if (k
< pivotIndex
) {
97 rightIndex
= pivotIndex
- 1
99 leftIndex
= pivotIndex
+ 1
104 function selectRecursion (
109 compare
= defaultComparator
,
110 pivotIndexSelect
= defaultPivotIndexSelect
112 if (leftIndex
=== rightIndex
) return array
[leftIndex
]
113 let pivotIndex
= pivotIndexSelect(leftIndex
, rightIndex
)
114 pivotIndex
= partition(array
, leftIndex
, rightIndex
, pivotIndex
, compare
)
115 if (k
=== pivotIndex
) {
117 } else if (k
< pivotIndex
) {
118 return selectRecursion(array
, k
, leftIndex
, pivotIndex
- 1, compare
)
120 return selectRecursion(array
, k
, pivotIndex
+ 1, rightIndex
, k
, compare
)
124 function quickSelectLoop (tasksMap
) {
125 const tasksArray
= Array
.from(tasksMap
)
127 return selectLoop(tasksArray
, 0, 0, tasksArray
.length
- 1, (a
, b
) => {
132 function quickSelectLoopRandomPivot (tasksMap
) {
133 const tasksArray
= Array
.from(tasksMap
)
139 tasksArray
.length
- 1,
143 randomPivotIndexSelect
147 function quickSelectRecursion (tasksMap
) {
148 const tasksArray
= Array
.from(tasksMap
)
150 return selectRecursion(tasksArray
, 0, 0, tasksArray
.length
- 1, (a
, b
) => {
155 function quickSelectRecursionRandomPivot (tasksMap
) {
156 const tasksArray
= Array
.from(tasksMap
)
158 return selectRecursion(
162 tasksArray
.length
- 1,
166 randomPivotIndexSelect
172 Benchmark
.add('Loop select', () => {
175 Benchmark
.add('Array sort select', () => {
176 arraySortSelect(tasksMap
)
178 Benchmark
.add('Quick select loop', () => {
179 quickSelectLoop(tasksMap
)
181 Benchmark
.add('Quick select loop with random pivot', () => {
182 quickSelectLoopRandomPivot(tasksMap
)
184 Benchmark
.add('Quick select recursion', () => {
185 quickSelectRecursion(tasksMap
)
187 Benchmark
.add('Quick select recursion with random pivot', () => {
188 quickSelectRecursionRandomPivot(tasksMap
)