Skip to content

Instantly share code, notes, and snippets.

@antenando
Forked from ebidel/sw_caching_size.js
Created December 15, 2017 23:46
Show Gist options
  • Save antenando/821f96c0fff4e18211e0843f3f83a9b1 to your computer and use it in GitHub Desktop.
Save antenando/821f96c0fff4e18211e0843f3f83a9b1 to your computer and use it in GitHub Desktop.
Print service worker cache sizes and overall bytes cached.
/**
* @author ebidel@ (Eric Bidelman)
* License Apache-2.0
*/
// Prints the size of each cache in the Cache Storage API and the overall bytes cached.
async function getCacheStoragesAssetTotalSize() {
// Note: opaque (i.e. cross-domain, without CORS) responses in the cache will return a size of 0.
const cacheNames = await caches.keys();
let total = 0;
const sizePromises = cacheNames.map(async cacheName => {
const cache = await caches.open(cacheName);
const keys = await cache.keys();
let cacheSize = 0;
await Promise.all(keys.map(async key => {
const response = await cache.match(key);
const buffer = await response.arrayBuffer();
total += buffer.byteLength;
cacheSize += buffer.byteLength;
}));
console.log(`Cache ${cacheName}: ${cacheSize} bytes`);
});
await Promise.all(sizePromises);
return `Total Cache Storage: ${total} bytes`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment