# Change the priority of queued work

> An indexed priority queue finds a queued item by its value and moves it in place.

**Few items**

```ts
import { minPriorityQueue, numberOrder } from 'knotkit/heap'

// [priority, task]: the lowest priority runs first.
const queue = minPriorityQueue(numberOrder, [
  [5, 'render'],
  [1, 'parse'],
  [3, 'layout'],
])

queue.updateKey(0, 'layout') // a user is now waiting on layout

queue.pop() // 'layout'
queue.pop() // 'parse'
queue.size // 1
```

**Thousands of items**

```ts
import { minPriorityQueue, numberOrder } from 'knotkit/heap'
import { splitmix32 } from 'knotkit/prng'
import { shuffled } from 'knotkit/sampling'

// 10,000 queued tasks, handed to the queue in shuffled order.
const tasks: [number, string][] = Array.from({ length: 10_000 }, (_, index) => [(index % 100) + 1, `task-${index}`])
const queue = minPriorityQueue(numberOrder, shuffled(splitmix32(2026), tasks))

// Key 0 is lower than every queued key, so this task is next out.
queue.updateKey(0, 'task-7777') // a user is now waiting on task 7777

queue.size // 10000
queue.pop() // 'task-7777'
queue.size // 9999
```

Use an indexed queue when jobs can change priority while they wait. The first key lookup or update builds the index in `O(n)`. After that, a binary queue finds an entry by value and changes its priority in `O(log n)`.

**Modules:** [`knotkit/heap`](/modules/heap/) · [API reference](/reference/knotkit/heap/)
