Skip to content

Instantly share code, notes, and snippets.

@morten-olsen
Last active August 29, 2015 14:16
Show Gist options
  • Save morten-olsen/53d78ed394aa695b1c80 to your computer and use it in GitHub Desktop.
Save morten-olsen/53d78ed394aa695b1c80 to your computer and use it in GitHub Desktop.
Calculations of an compression algorithm which can (should, very slightly) compress random data
console.clear();
var targetReduction = 22, // Decompressor in KB
patternBytes = 1,
sequenceLengths = [4,5],
blocksize = 4; // Block size of FS in KB
// ---- Lot of technical mumbo-jumbo
var logs = [],
totalCompression = 0,
totalBytes = 0,
i;
logs.push('Original target: ' + targetReduction + ' KB');
if (targetReduction % blocksize != 0) targetReduction += blocksize - (targetReduction % blocksize);
logs.push('Actual target: ' + targetReduction + ' KB');
logs.push('Patterns: ' + patternBytes * 256);
targetReduction = targetReduction * 1024;
filesize = 1000 * 1024 * 1024; // Uses 1 GB as base for calculation
for (i = sequenceLengths.length - 1; i >= 0; i--) {
var bytes = 1 / Math.pow(256, sequenceLengths[i]) * Math.pow(256, patternBytes) * filesize,
reduction = (bytes * sequenceLengths[i]),
body = filesize - reduction,
header = (bytes * (patternBytes + Math.ceil(bytes / 256))),
compression = reduction - header;
totalBytes += bytes;
totalCompression += compression;
filesize -= reduction * 2;
logs.push('Sequence: ' + sequenceLengths[i] + ': ' + (compression / 1024).toFixed(5) + ' KB')
};
logs.push('Total reduction pr gb: ' + (totalCompression / 1024).toFixed(5) + ' KB');
logs.push('Size needed: ' + (targetReduction / totalCompression).toFixed(5) + ' GB');
if (!!console && !!console.log) console.log(logs.join('\r\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment