1 import Benchmark from 'benchmark'
2 import { LIST_FORMATTER, generateRandomInteger } from '../benchmarks-utils.mjs'
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) {
20 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
170 new Benchmark.Suite('Least used worker tasks distribution')
171 .add('Loop select', () => {
174 .add('Array sort select', () => {
175 arraySortSelect(tasksMap)
177 .add('Quick select loop', () => {
178 quickSelectLoop(tasksMap)
180 .add('Quick select loop with random pivot', () => {
181 quickSelectLoopRandomPivot(tasksMap)
183 .add('Quick select recursion', () => {
184 quickSelectRecursion(tasksMap)
186 .add('Quick select recursion with random pivot', () => {
187 quickSelectRecursionRandomPivot(tasksMap)
189 .on('cycle', event => {
190 console.info(event.target.toString())
192 .on('complete', function () {
194 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))