Created
September 28, 2021 11:15
-
-
Save coffee-mug/8169affc276ca815afb3a1604eaab700 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns an array of launch element (rule, dataelements, ...) sorted | |
// by bytes length | |
function dataElementsWeights() { | |
// We use an object so that total is passed by reference (using an int would | |
// pass it by value, thus leading to a different value in each object instead | |
// of the total value) | |
var totalWeight = { | |
total: 0 | |
}; | |
return Object.keys(window._satellite._container.dataElements).map(name => { | |
let weight = window._satellite._container.dataElements[name].settings.source ? _satellite._container.dataElements[name].settings.source.toString().length : 0; | |
totalWeight.total += weight; | |
return { | |
name: name, | |
category: "dataElements", | |
weight: weight, | |
total: totalWeight, | |
} | |
}) | |
} | |
async function rulesWithCustomJSWeights() { | |
// See in dataElementsWeights for explanation | |
var totalWeight = { | |
total: 0 | |
}; | |
let results = []; | |
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled | |
await window._satellite._container.rules.map(rule => { | |
return ["actions", "conditions", "events"].map(component => { | |
return rule[component].map(async comp => { | |
// Only check rules with custom code (either in action, conditions or events) | |
await new Promise((resolve, reject) => { | |
if (/customCode\.js/.test(comp.modulePath)) { | |
// ok strangely custom code in conditions is not stored in a dedicated file. | |
switch (component) { | |
case "actions": | |
let script = fetch(comp.settings.source).then(response => response.text()).then(text => { | |
let weight = text.length; | |
totalWeight.total += weight; | |
results.push({ | |
name: rule.name, | |
category: component, | |
weight: weight, | |
total: totalWeight, | |
url: comp.settings.source, | |
}); | |
}); | |
break; | |
case "conditions": | |
// Not taking into account every cases but we're only interested in custom code for now, as it is | |
// usually where most of the ugly and heavy stuff reside in Launch. | |
let weight = typeof comp.settings.source == "function" ? comp.settings.source.toString().length : 0; | |
totalWeight.total += weight; | |
results.push({ | |
name: rule.name, | |
category: component, | |
weight: weight, | |
total: totalWeight, | |
url: comp.settings.source, | |
}); | |
} | |
} | |
}) | |
}) | |
}) | |
}) | |
return await results; | |
} | |
function sortDescending(list, attribute) { | |
return list.sort((a, b) => b[attribute] - a[attribute]); | |
} | |
// Log the 3 heaviest data elements | |
console.log("--- 3 Heaviest custom code data elements ---"); | |
const dew = dataElementsWeights(); | |
console.log(sortDescending(dew, "weight").slice(0, 3)); | |
console.log("--- data elements total weight (uncompressed) --- "); | |
console.log(`${(sortDescending(dew, "weight")[0].total.total / 1000).toFixed(2)} kB`); | |
// in snippet code, await calls must be made inside an async function. | |
async function logRulesStats() { | |
const rulesWeights = await rulesWithCustomJSWeights(); | |
// Ugly but i can't figure out yet how to use await to let me access | |
// the rulesWeights array only once it is filled up already. | |
setTimeout(() => { | |
console.log("--- 3 heaviest rules with custom code ---"); | |
console.log(sortDescending(rulesWeights, "weight").slice(0, 3)); | |
console.log("--- rules with custom code total weight (uncompressed) ---"); | |
console.log(`${(sortDescending(rulesWeights, "weight")[0].total.total / 1000).toFixed(2)} kB`); | |
}, 1000); | |
} | |
logRulesStats(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment