I’ve just noticed strange behaviour with rendering before script is being performed. I always thought, that there is no chance for browsers to dequeue a task from render queue and execute it before script tag, since this is a single thread. And as you will see, it’s not true. There is a simple example of code below:
console.log('SCRIPT EXECUTED')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>HTML</title>
<script>
requestAnimationFrame(function animate(time) {
console.log('RAF', performance.now());
});
</script>
</head>
<body>
<h1>Hello world</h1>
<script src="./script.js"></script>
</body>
</html>
One may see inconsistent output in console. Sometimes render comes up before script and vice versa. It may cause to flickering in UI for example.
Could anyone give a meaningful explanations of this?