Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created February 19, 2020 08:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/7bab57a330e4a318049fe2680ba09ed3 to your computer and use it in GitHub Desktop.
Save WebReflection/7bab57a330e4a318049fe2680ba09ed3 to your computer and use it in GitHub Desktop.
A benchmark to see how many items can be stored using WebCompressor
const findLimit = async (type, create) => {
console.log(
`%c ${type} benchmark `,
`font-weight:bold;color:white;background-color:black`
);
localStorage.clear();
let i = 0;
let length = 0;
while (true) {
try {
if (i < 1)
console.time(`${type} storing time`);
const value = (await create()).toString();
localStorage.setItem(i, value);
if (i++ < 1)
console.timeEnd(`${type} storing time`);
length += value.length;
}
catch (limit) {
console.log(
`%c${type}`,
`font-weight:bold`,
`stored ${i} items with overall length of ${length}`
);
break;
}
}
}
const value = () => String(Math.random() + new Date).repeat(1024);
import('https://unpkg.com/web-compressor?module').then(
async ({default: WebCompressor}) => {
await findLimit('plain', value);
const b64deflate = new WebCompressor().compress;
await findLimit('base64 + deflate', () => b64deflate(value()));
const b64gzip = new WebCompressor('gzip').compress;
await findLimit('base64 + gzip', () => b64gzip(value()));
const utf16deflate = new WebCompressor('deflate', 'utf-16').compress;
await findLimit('utf-16 + deflate', () => utf16deflate(value()));
const utf16gzip = new WebCompressor('gzip', 'utf-16').compress;
await findLimit('utf-16 + gzip', () => utf16gzip(value()));
}
);
@WebReflection
Copy link
Author

Validation Test

const value = () => String(Math.random() + new Date).repeat(1024);

import('https://unpkg.com/web-compressor?module').then(
  async ({default: WebCompressor}) => {
    const {compress, decompress} = new WebCompressor('deflate', 'utf-16');
    localStorage.clear();
    const entry = value();
    console.time('validation test');
    localStorage.setItem('test', await compress(entry));
    console.assert(
      await decompress(localStorage.getItem('test')) === entry,
      'unexpected entry'
    );
    console.timeEnd('validation test');
  }
);

@WebReflection
Copy link
Author

WebReflection commented Feb 19, 2020

Results

plain benchmark

plain storing time: 1.3828125ms
plain stored 60 items with overall length of 5166080

base64 + deflate benchmark

base64 + deflate storing time: 10.251953125ms
base64 + deflate stored 9706 items with overall length of 5205052

base64 + gzip benchmark

base64 + gzip storing time: 8.6259765625ms
base64 + gzip stored 9431 items with overall length of 5206148

utf-16 + deflate benchmark

utf-16 + deflate storing time: 6.224853515625ms
utf-16 + deflate stored 25385 items with overall length of 5126905

utf-16 + gzip benchmark

utf-16 + gzip storing time: 14.4921875ms
utf-16 + gzip stored 24668 items with overall length of 513063

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment