Skip to content

Commit e64b85a

Browse files
committed
chore: add quick and dirty performance test
1 parent a983cea commit e64b85a

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@toolbuilder/priority-buffer",
3-
"description": "Priority queue buffer with limited capacity where implemented methods match Array signature.",
3+
"description": "Priority queue buffer with limited capacity where push/shift/length match the Array API.",
44
"version": "0.1.0",
55
"keywords": [
66
"priority",
@@ -9,7 +9,11 @@
99
],
1010
"license": "MIT",
1111
"main": "index.js",
12-
"module": "src/priority-buffer.js",
12+
"module": "./src/priority-buffer.js",
13+
"exports": {
14+
"import": "./src/priority-buffer.js",
15+
"default": "./index.js"
16+
},
1317
"files": [
1418
"src"
1519
],
@@ -20,7 +24,8 @@
2024
"check:src": "eslint src test",
2125
"check:test": "run-s test",
2226
"check:uncommitted": "uncommitted",
23-
"prerelease": "run-s check",
27+
"docs": "pnpx documentation build src/priority-buffer.js -f md -o docs/priority-buffer.md",
28+
"prerelease": "run-s docs check",
2429
"release": "standard-version",
2530
"test": "tape -r esm \"test/*test.js\""
2631
},
@@ -33,6 +38,7 @@
3338
"devDependencies": {
3439
"@toolbuilder/eslint-config": "^0.1.4",
3540
"@toolbuilder/package-json-lint-config": "^0.1.0",
41+
"@toolbuilder/ring-buffer": "^1.1.2",
3642
"@toolbuilder/ring-buffer-tests": "^0.1.0",
3743
"@toolbuilder/rollup-config-pkgtest": "^0.1.2",
3844
"eslint": "^7.14.0",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/priority-buffer.perf.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { PriorityBuffer } from '../src/priority-buffer.js'
2+
import { RingBuffer } from '@toolbuilder/ring-buffer'
3+
4+
const iterations = 50000000
5+
const length = 100
6+
7+
const time = (queue) => {
8+
for (let i = 0; i < length; ++i) {
9+
queue.push(Math.random(i))
10+
}
11+
for (let i = 0; i < iterations; ++i) {
12+
queue.shift()
13+
queue.push(Math.random(i))
14+
}
15+
}
16+
17+
console.time('RingBuffer')
18+
time(new RingBuffer(length)) // unprioritized
19+
console.timeEnd('RingBuffer')
20+
21+
console.time('array')
22+
time([]) // unprioritized
23+
console.timeEnd('array')
24+
25+
console.time('PriorityBuffer')
26+
const comparator = (a, b) => a - b
27+
time(new PriorityBuffer(comparator, length))
28+
console.timeEnd('PriorityBuffer')

test/priority-buffer.test.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
import { test } from 'zora'
22
import { PriorityBuffer } from '../src/priority-buffer.js'
33
import { RingBufferDriver } from '@toolbuilder/ring-buffer-tests'
4+
import { SimplePriorityBuffer } from './simple-priority-buffer.js'
45

56
// comparator for test
67
const comparator = (a, b) => b - a
78

8-
/**
9-
* Super simple priority buffer to compare PriorityBuffer against.
10-
* Violates Liskov substitutability, but matches PriorityBuffer
11-
* behavior and API as required.
12-
*/
13-
class SimplisticPriorityBuffer extends Array {
14-
constructor (comparator, capacity) {
15-
super()
16-
this.capacity = capacity
17-
this.comparator = comparator
18-
}
19-
20-
front () { return this[0] }
21-
back () { return this[this.length - 1] }
22-
23-
push (value) {
24-
super.push(value)
25-
super.sort(this.comparator)
26-
if (this.length > this.capacity) this.pop()
27-
return this.length
28-
}
29-
}
30-
319
// Method collects state from buffers for comparing buffer states
3210
const getBufferState = (priorityBuffer) => {
3311
return {
@@ -41,7 +19,7 @@ const getBufferState = (priorityBuffer) => {
4119

4220
// RingBufferDriver options for this test
4321
const driverOptions = {
44-
exemplarFactory: (capacity) => new SimplisticPriorityBuffer(comparator, capacity),
22+
exemplarFactory: (capacity) => new SimplePriorityBuffer(comparator, capacity),
4523
getState: getBufferState,
4624
methodPairs: [['push', 'shift']] // PriorityBuffer does not support unshift/pop
4725
}

test/simple-priority-buffer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Super simple priority buffer to act as an exemplar for PriorityBuffer tests.
3+
* It matches the expected behavior of PriorityBuffer, but is extremely slow.
4+
*
5+
* It also violates Liskov substitutability for Array, but that's not an issue here.
6+
*/
7+
export class SimplePriorityBuffer extends Array {
8+
constructor (comparator, capacity) {
9+
super()
10+
this.capacity = capacity
11+
this.comparator = comparator
12+
}
13+
14+
front () { return this[0] }
15+
back () { return this[this.length - 1] }
16+
17+
push (value) {
18+
super.push(value)
19+
super.sort(this.comparator)
20+
if (this.length > this.capacity) this.pop()
21+
return this.length
22+
}
23+
}

0 commit comments

Comments
 (0)