-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
52 lines (45 loc) · 1.48 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { compress } from 'snappyjs'
import { snappyUncompressor } from './hysnappy.js'
const fileSize = 200_000_000
const compressed = time(`generate and compress ${fileSize.toLocaleString()}`, () => {
// Generate input array with random data
const input = new Uint8Array(fileSize)
for (let i = 0; i < fileSize; i++) {
input[i] = Math.floor(Math.random() * 16)
}
return compress(input)
})
console.log(`compressed ${fileSize.toLocaleString()} bytes to ${compressed.length.toLocaleString()} bytes`)
const snappyUncompress = snappyUncompressor()
timeWithStdDev('uncompress wasm', () => snappyUncompress(compressed, fileSize))
// time('uncompress snappyjs', () => uncompress(compressed, fileSize))
/**
* @param {string} name
* @param {() => any} fn
* @returns {any}
*/
function time(name, fn) {
const start = performance.now()
const output = fn()
const ms = performance.now() - start
console.log(`${name} took ${ms} ms`)
return output
}
/**
* @param {string} name
* @param {() => void} fn
* @param {number} iterations
*/
function timeWithStdDev(name, fn, iterations = 20) {
const times = []
for (let i = 0; i < iterations; i++) {
const start = performance.now()
fn()
const ms = performance.now() - start
times.push(ms)
}
const mean = times.reduce((a, b) => a + b, 0) / times.length
const variance = times.reduce((a, b) => a + (b - mean) ** 2, 0) / times.length
const stdDev = Math.sqrt(variance)
console.log(`${name} took ${mean} ms ± ${stdDev} ms`)
}