Skip to content

Instantly share code, notes, and snippets.

@kdzwinel
Created January 25, 2018 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdzwinel/c0f3953faeedc768d642894044b92328 to your computer and use it in GitHub Desktop.
Save kdzwinel/c0f3953faeedc768d642894044b92328 to your computer and use it in GitHub Desktop.
__LIGHTHOUSE_JSON__ analysis script
(function() {
function strSize(obj) {
const string = JSON.stringify(obj);
return string ? string.length : 0;
}
const lhj = __LIGHTHOUSE_JSON__;
const fullSize = strSize(lhj);
const maxLevels = 5;
function analyzeKeys(obj, level = 0) {
Object.keys(obj)
.map(field => ({
name: field,
size: strSize(obj[field])
}))
.sort((a, b) => b.size - a.size)
.forEach(field => {
const percentage = Math.round((field.size * 100)/fullSize);
const value = field.name === 'name' && (typeof obj[field.name] === 'string') ? ` ${obj[field.name]}` : '';
const label = `${' '.repeat(level)}↪ ${field.name}:${value} ${percentage}%`;
if (percentage > 0 && level < maxLevels) {
console.groupCollapsed(label);
analyzeKeys(obj[field.name], level + 1);
console.groupEnd();
} else {
console.log(label);
}
});
}
analyzeKeys(lhj);
})();
@kdzwinel
Copy link
Author

Run in the console of the raport page to get a following breakdown of __LIGHTHOUSE_JSON__:

screen shot 2018-01-25 at 15 03 18

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