Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 49beeca

Browse files
use binary search in the prioritized task executor
1 parent 0e6e1f7 commit 49beeca

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

src/prioritizedTaskExecutor.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,40 @@ export class PrioritizedTaskExecutor {
3535
fn(() => {
3636
this.currentPoolSize--
3737
if (this.queue.length > 0) {
38-
this.queue.sort((a, b) => b.priority - a.priority)
3938
const item = this.queue.shift()
4039
this.execute(item!.priority, item!.fn)
4140
}
4241
})
4342
} else {
44-
this.queue.push({ priority, fn })
43+
if (this.queue.length == 0) {
44+
this.queue.push({ priority, fn })
45+
} else {
46+
// insert the item in the queue using binary search
47+
let left = 0
48+
let right = this.queue.length
49+
let mid = () => {
50+
return Math.floor(left + (right - left) / 2)
51+
}
52+
while (true) {
53+
let index = mid()
54+
let value = this.queue[index].priority
55+
console.log(left, right, index, value)
56+
if (value == priority) {
57+
this.queue.splice(index, 0, { priority, fn })
58+
break
59+
}
60+
if (left == right) {
61+
this.queue.splice(left, 0, { priority, fn })
62+
break
63+
}
64+
65+
if (value > priority) {
66+
left = index
67+
} else {
68+
right = index
69+
}
70+
}
71+
}
4572
}
4673
}
4774

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
import * as tape from 'tape'
22
import { PrioritizedTaskExecutor } from '../src/prioritizedTaskExecutor'
33

4-
const taskExecutor = new PrioritizedTaskExecutor(2)
4+
tape('prioritized task executor test', function (t: any) {
5+
t.test('should execute tasks in the right order', (st: any) => {
6+
const taskExecutor = new PrioritizedTaskExecutor(2)
7+
const tasks = [1, 2, 3, 4]
8+
const callbacks = [] as any
9+
const executionOrder = [] as any
10+
tasks.forEach(function (task) {
11+
taskExecutor.execute(task, function (cb: Function) {
12+
executionOrder.push(task)
13+
callbacks.push(cb)
14+
})
15+
})
516

6-
tape('prioritized task executor test', function (t) {
7-
const tasks = [1, 2, 3, 4]
8-
const callbacks = [] as any
9-
const executionOrder = [] as any
10-
tasks.forEach(function (task) {
11-
taskExecutor.execute(task, function (cb: Function) {
12-
executionOrder.push(task)
13-
callbacks.push(cb)
17+
callbacks.forEach(function (callback: Function) {
18+
callback()
1419
})
15-
})
1620

17-
callbacks.forEach(function (callback: Function) {
18-
callback()
21+
const expectedExecutionOrder = [1, 2, 4, 3]
22+
st.deepEqual(executionOrder, expectedExecutionOrder)
23+
st.end()
1924
})
2025

21-
const expectedExecutionOrder = [1, 2, 4, 3]
22-
t.deepEqual(executionOrder, expectedExecutionOrder)
23-
t.end()
26+
t.test('should queue tasks in the right order', (st: any) => {
27+
const priorityList = [0, 1, 0, 2, 0, 1, 0, 2, 2, 1]
28+
const PTE = new PrioritizedTaskExecutor(0) // this ensures that no task actually gets executed, so this essentially just checks the sort algorithm
29+
priorityList.map((priority) => {
30+
PTE.execute(priority, () => {})
31+
})
32+
// have to cast the PTE as <any> to access the private queue
33+
st.deepEqual(
34+
(<any>PTE).queue.map((task: any) => task.priority),
35+
priorityList.sort((a: any, b: any) => b - a),
36+
)
37+
st.end()
38+
})
2439
})

0 commit comments

Comments
 (0)