Last active
July 27, 2021 22:47
-
-
Save foolmoron/f70ffd2e9999c2851e24a8f3328c742b to your computer and use it in GitHub Desktop.
Node.js compression tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zlib = require('zlib'); | |
const perf_hooks = require('perf_hooks') | |
function t() { | |
return perf_hooks.performance.now() | |
} | |
const BUFFER_SIZE = 1024*1024 | |
const TRIALS = 20 | |
const buffer = new ArrayBuffer(BUFFER_SIZE) | |
const array = new Int8Array(buffer) | |
const patterns = [ | |
['solid', i => 88], | |
['regular patt', i => i % 117], | |
['single sin', i => Math.sin(i*5)*256], | |
['double sin', i => Math.sin(i*5)*Math.sin(i*91)*256], | |
['checker-ish', i => { | |
const x = i % 1024 | |
const y = Math.floor(i / 1024) | |
return [5, 29, 111, 244][(x + y) % 4] | |
}], | |
['32x32 solids', i => { | |
const x = i % 1024 | |
const y = Math.floor(i / 1024) | |
const bx = Math.floor(x / 32) | |
const by = Math.floor(y / 32) | |
return by*32 + bx | |
}], | |
['4-random', i => { | |
const x = i % 1024 | |
const y = Math.floor(i / 1024) | |
return [5, 29, 111, 244][Math.floor(Math.random()*4)] | |
}], | |
] | |
const methods = [ | |
['gzip', zlib.gzipSync, zlib.gunzipSync], | |
// ['defl', zlib.deflateSync, zlib.inflateSync], // basically identical to gzip | |
['brot', zlib.brotliCompressSync, zlib.brotliDecompressSync], | |
] | |
for (const [pName, p] of patterns) { | |
for (const [mName, comp, decomp] of methods) { | |
array.forEach((_, i) => array[i] = p(i)) | |
let compSize = 0, decompTime = 0, compTime = 0 | |
for (let trial = 0; trial < TRIALS; trial++) { | |
const t0 = t() | |
const compressed = comp(buffer) | |
compSize += compressed.byteLength / TRIALS | |
const t1 = t() | |
decomp(compressed) | |
decompTime += (t() - t1) / TRIALS | |
compTime += (t1 - t0) / TRIALS | |
} | |
console.log(`${mName} - ${pName}:\t\t${compTime.toFixed(1)}ms \t\t${decompTime.toFixed(1)}ms \t\t${(compSize/1024).toFixed(2)}kb \t\t${(array.length/compSize).toFixed(2)}x`) | |
} | |
} | |
console.log('Done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment