User:Alexis Jazz/lz-string/bench.js

// Compare compression ratio and speed of lz-string and pako. Pako is faster and has a higher compression ratio. It's a considerably larger library though, and mediaWiki currently only provides the compression side of pako. (https://phabricator.wikimedia.org/T312720) //This script is irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz. /*globals $:false,mw:false,LZString:false*/ window.testcompress = {}; var testcompress = window.testcompress; mw.loader.getScript('https://en.wikipedia.org/w/index.php?title=User:Alexis_Jazz/lz-string.js&action=raw&ctype=text/javascript&oldid=1097494057').then( function{

mw.loader.using('mediawiki.deflate',function{

testcompress.showresult = function(method,time,output){ console.log(method+'\t'+time+' ms\t'+output.length+'\t'+Math.round(output.length/1000)+'\t'+(output.length/(testcompress.data.length/100)).toFixed(2)+'\t'+new Blob([output]).size+'\t'+Math.round(new Blob([output]).size/1000)+'\t'+(new Blob([output]).size/(new Blob([testcompress.data]).size/100)).toFixed(2)); };

testcompress.mwinflate = function ( data, dataArr, int ) { data = atob(data.slice(11)); dataArr = []; for(int=0;int<data.length;int++) { dataArr.push(data.charCodeAt(int)); }	dataArr = Uint8Array.from(dataArr); return new TextDecoder.decode(pako.inflateRaw(dataArr)); };

testcompress.run = function(num,data){

console.log('\n\nMethod\t\t\tTime\tLength\tK\t%\tBytes\tK\t%');

testcompress.datestart = new Date.getTime; testcompress.data = ''; if ( data ) { testcompress.data = data; } else { for (testcompress.int=0;testcompress.int<num;testcompress.int++){ testcompress.data = testcompress.data+$('body')[0].outerHTML.replace(/a/g,testcompress.int); //sort of real-life example. the .replace makes it less repetitive } }

testcompress.dateend = new Date.getTime; testcompress.generatetook=testcompress.dateend - testcompress.datestart; testcompress.showresult('Uncompressed\t',testcompress.generatetook,testcompress.data);

testcompress.datestart = new Date.getTime; testcompress.compressedlzstring16 = LZString.compressToUTF16(testcompress.data); testcompress.dateend = new Date.getTime; testcompress.lzstring16benchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('lz-string (UTF16)',testcompress.lzstring16benchtook,testcompress.compressedlzstring16);

testcompress.datestart = new Date.getTime; testcompress.decompressedlzstring16 = LZString.decompressFromUTF16(testcompress.compressedlzstring16); testcompress.dateend = new Date.getTime; testcompress.lzstring16benchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('lz-s (UTF16) inflate',testcompress.lzstring16benchtook,testcompress.decompressedlzstring16);

testcompress.datestart = new Date.getTime; testcompress.compressedlzstringB64 = LZString.compressToBase64(testcompress.data); testcompress.dateend = new Date.getTime; testcompress.lzstringbenchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('lz-string (base64)',testcompress.lzstringbenchtook,testcompress.compressedlzstringB64);

testcompress.datestart = new Date.getTime; testcompress.decompressedlzstringB64 = LZString.decompressFromBase64(testcompress.compressedlzstringB64); testcompress.dateend = new Date.getTime; testcompress.lzstringbenchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('lz-s (base64) inflate',testcompress.lzstringbenchtook,testcompress.decompressedlzstringB64);

testcompress.datestart = new Date.getTime; testcompress.compressedpako = mw.deflate(testcompress.data); testcompress.dateend = new Date.getTime; testcompress.pakobenchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('mw.deflate/pako\t',testcompress.pakobenchtook,testcompress.compressedpako);

if ( ( ( typeof Bawl != 'undefined' && Bawl.pako ) || ( typeof pako != 'undefined' && pako.inflateRaw ) ) && typeof TextDecoder == 'function' ) { if ( typeof pako == 'undefined' ) { pako = Bawl.pako; }	testcompress.datestart = new Date.getTime; testcompress.decompressedpako = testcompress.mwinflate(testcompress.compressedpako); testcompress.dateend = new Date.getTime; testcompress.pakobenchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('inflate/pako\t',testcompress.pakobenchtook,testcompress.decompressedpako); }

if ( typeof Bawl != 'undefined' && Bawl.pako ) { testcompress.datestart = new Date.getTime; testcompress.compressedzlib = Bawl.deflate(testcompress.data,'zlib_plain'); testcompress.dateend = new Date.getTime; testcompress.zlibbenchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('deflate/zlib\t',testcompress.zlibbenchtook,testcompress.compressedzlib);

testcompress.datestart = new Date.getTime; testcompress.decompressedzlib = Bawl.inflate(testcompress.compressedzlib); testcompress.dateend = new Date.getTime; testcompress.zlibuncbenchtook=testcompress.dateend - testcompress.datestart; testcompress.showresult('inflate/zlib\t',testcompress.zlibuncbenchtook,testcompress.decompressedzlib);

}

};

testcompress.run(1); testcompress.run(5); testcompress.run(10);

}); });