Skip to content

Instantly share code, notes, and snippets.

@kdzwinel kdzwinel/lhjson.js
Created Jan 25, 2018

Embed
What would you like to do?
__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

This comment has been minimized.

Copy link
Owner Author

commented Jan 25, 2018

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
You can’t perform that action at this time.