Skip to content

Commit

Permalink
Benchmark 1gb file
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Feb 21, 2024
1 parent 92cece7 commit 4bf1e52
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import { compress, uncompress } from 'snappyjs'
import { snappyUncompress as uncompressWasm } from './hysnappy.js'

const fileSize = 200_000_000

const compressed = await 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 output = new Uint8Array(fileSize)

await timeWithStdDev('uncompress wasm', () => uncompressWasm(compressed, output))

// await time('uncompress snappyjs', () => uncompress(compressed, output))

async function time(name, fn) {
const start = performance.now()
const output = await fn()
const ms = performance.now() - start
console.log(`${name} took ${ms} ms`)
return output
}

async function timeWithStdDev(name, fn, iterations = 20) {
const times = []
for (let i = 0; i < iterations; i++) {
const start = performance.now()
await 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`)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"eslint": "8.56.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jsdoc": "48.1.0",
"snappyjs": "0.7.0",
"vitest": "1.3.1"
}
}

0 comments on commit 4bf1e52

Please sign in to comment.