1 const Benchmark
= require('benchmark')
2 const { generateRandomInteger
, LIST_FORMATTER
} = require('./benchmark-utils')
4 const suite
= new Benchmark
.Suite()
6 function generateRandomTasksMap (
8 maxNumberOfTasksPerWorker
= 10
11 for (let i
= 0; i
< numberOfWorkers
; i
++) {
12 const task
= [i
, generateRandomInteger(maxNumberOfTasksPerWorker
)]
15 return new Map(tasksArray
)
18 const tasksMap
= generateRandomTasksMap(60, 20)
20 function loopSelect (tasksMap
) {
21 let minValue
= Infinity
23 for (const [key
, value
] of tasksMap
) {
26 } else if (value
< minValue
) {
31 return [minKey
, minValue
]
34 function arraySortSelect (tasksMap
) {
35 const tasksArray
= Array
.from(tasksMap
)
36 return tasksArray
.sort((a
, b
) => {
39 } else if (a
[1] > b
[1]) {
46 const defaultComparator
= (a
, b
) => {
50 const defaultPivotIndexSelect
= (leftIndex
, rightIndex
) => {
51 return leftIndex
+ Math
.floor((rightIndex
- leftIndex
) / 2)
54 const randomPivotIndexSelect
= (leftIndex
, rightIndex
) => {
55 return generateRandomInteger(leftIndex
, rightIndex
)
58 function swap (array
, index1
, index2
) {
59 const tmp
= array
[index1
]
60 array
[index1
] = array
[index2
]
69 compare
= defaultComparator
71 const pivotValue
= array
[pivotIndex
]
72 swap(array
, pivotIndex
, rightIndex
)
73 let storeIndex
= leftIndex
74 for (let i
= leftIndex
; i
< rightIndex
; i
++) {
75 if (compare(array
[i
], pivotValue
)) {
76 swap(array
, storeIndex
, i
)
80 swap(array
, rightIndex
, storeIndex
)
89 compare
= defaultComparator
,
90 pivotIndexSelect
= defaultPivotIndexSelect
93 if (leftIndex
=== rightIndex
) return array
[leftIndex
]
94 let pivotIndex
= pivotIndexSelect(leftIndex
, rightIndex
)
95 pivotIndex
= partition(array
, leftIndex
, rightIndex
, pivotIndex
, compare
)
96 if (k
=== pivotIndex
) {
98 } else if (k
< pivotIndex
) {
99 rightIndex
= pivotIndex
- 1
101 leftIndex
= pivotIndex
+ 1
106 function selectRecursion (
111 compare
= defaultComparator
,
112 pivotIndexSelect
= defaultPivotIndexSelect
114 if (leftIndex
=== rightIndex
) return array
[leftIndex
]
115 let pivotIndex
= pivotIndexSelect(leftIndex
, rightIndex
)
116 pivotIndex
= partition(array
, leftIndex
, rightIndex
, pivotIndex
, compare
)
117 if (k
=== pivotIndex
) {
119 } else if (k
< pivotIndex
) {
120 return selectRecursion(array
, k
, leftIndex
, pivotIndex
- 1, compare
)
122 return selectRecursion(array
, k
, pivotIndex
+ 1, rightIndex
, k
, compare
)
126 function quickSelectLoop (tasksMap
) {
127 const tasksArray
= Array
.from(tasksMap
)
129 return selectLoop(tasksArray
, 0, 0, tasksArray
.length
- 1, (a
, b
) => {
134 function quickSelectLoopRandomPivot (tasksMap
) {
135 const tasksArray
= Array
.from(tasksMap
)
141 tasksArray
.length
- 1,
145 randomPivotIndexSelect
149 function quickSelectRecursion (tasksMap
) {
150 const tasksArray
= Array
.from(tasksMap
)
152 return selectRecursion(tasksArray
, 0, 0, tasksArray
.length
- 1, (a
, b
) => {
157 function quickSelectRecursionRandomPivot (tasksMap
) {
158 const tasksArray
= Array
.from(tasksMap
)
160 return selectRecursion(
164 tasksArray
.length
- 1,
168 randomPivotIndexSelect
172 // console.log(Array.from(tasksMap))
173 // console.log(loopSelect(tasksMap))
174 // console.log(arraySortSelect(tasksMap))
175 // console.log(quickSelectLoop(tasksMap))
176 // console.log(quickSelectLoopRandomPivot(tasksMap))
177 // console.log(quickSelectRecursion(tasksMap))
178 // console.log(quickSelectRecursionRandomPivot(tasksMap))
181 .add('Loop select', function () {
184 .add('Array sort select', function () {
185 arraySortSelect(tasksMap
)
187 .add('Quick select loop', function () {
188 quickSelectLoop(tasksMap
)
190 .add('Quick select loop with random pivot', function () {
191 quickSelectLoopRandomPivot(tasksMap
)
193 .add('Quick select recursion', function () {
194 quickSelectRecursion(tasksMap
)
196 .add('Quick select recursion with random pivot', function () {
197 quickSelectRecursionRandomPivot(tasksMap
)
199 .on('cycle', function (event
) {
200 console
.log(event
.target
.toString())
202 .on('complete', function () {
204 'Fastest is ' + LIST_FORMATTER
.format(this.filter('fastest').map('name'))
206 // eslint-disable-next-line no-process-exit