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 1d5680e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"eslint:recommended"
],
"parserOptions": {
"ecmaVersion": 12,
"ecmaVersion": 13,
"sourceType": "module"
},
"plugins": ["import", "jsdoc"],
Expand Down Expand Up @@ -48,7 +48,7 @@
"prefer-destructuring": ["warn", {"object": true, "array": false}],
"prefer-promise-reject-errors": "error",
"quotes": ["error", "single"],
"require-await": "error",
"require-await": "warn",
"semi": ["error", "never"],
"sort-imports": ["error", {
"ignoreDeclarationSort": true,
Expand Down
53 changes: 53 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

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

const fileSize = 200_000_000

const compressed = await time(`generate and compress ${fileSize.toLocaleString()}`, async () => {
// 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))

/**
* @param {string} name
* @param {() => Promise<any>} fn
* @returns {Promise<any>}
*/
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
}

/**
* @param {string} name
* @param {() => Promise<void>} fn
* @param {number} iterations
*/
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`)
}
4 changes: 4 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'snappyjs' {
function uncompress(compressed: Uint8Array, maxLength?: number): Uint8Array
function compress(uncompressed: Uint8Array): Uint8Array
}
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 1d5680e

Please sign in to comment.