<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="/favicon.ico">
<title>First Painting</title>
<style>
body {background-image: url('data:image/png;base64,BvdaSDKsddsa...')}
</style>
<script>
...
</script>
<link href="static/css/main.b804254e.css" rel="stylesheet">
<script type="text/javascript" src="static/js/lib.js"></script>
</head>
<body>
<ul>
<li>First Painting Time</li>
<li>Smooth Animation</li>
<li>Memory Usage</li>
</ul>
</body>
</html>
How to optimise following code for faster first painting?
<script>
const boxes = Array.from(document.getElementsByClassName('box'));
const update = () => {
boxes.forEach((box, i) => {
box.style.width = (box.getBoundingClientRect().width + 1) + 'px';
})
setTimeout(update, 0);
}
update();
</script>
How to optimise following code?
<script>
const container = document.getElementById('container');
const update = () => {
const fragment = document.createDocumentFragment();
for (let i = 0; i < 100; i++) {
const box = document.createElement('div');
fragment.appendChild(box);
}
container.appendChild(fragment);
}
update();
</script>
Will fragment help with performance?Why?
Understand Task Queue in JS
const fn = () => fn();
const fn = () => setTimeout(fn);
const fn = () => Promise.resolve(fn);
function displayHeightOnSelect() {
box.classList.add('hightlight');
container.innerHTML = box.offsetHeight;
}
Another Typical Issue
const metadata = {
container: document.body,
id: 'ad',
createAt: new Date()
}
const someAsycOperation = () => Promise.resolve();
const cleanup = (id) => {/*do something*/}
const execute = (metadata) => {
const { id, createAt } = metadata;
someAsycOperation().then(() => {
console.log(id, createAt);
window[id] = {
completeAt: new Date(),
cleanup: () => {}
};
});
}
Explicit Leak Caused By Closure