Inaccurate Timeout

DOM operations make timeouts inaccurate - the timeouts are supposed to happen after 50 ms, but do, in fact, take 12-15 ms longer than that

by Patrick Hund

HTML

<div id="start">
    Hi ho!
</div>

JavaScript

const timeout = 50;

function blub() {
    return new Promise(resolve => {
        console.log(`[PREBID_LOG] timeout to ${timeout} ms`); // PH_TODO
        const ms = Date.now();
        let done = false;
        const timeoutHandle = setTimeout(() => {
            // eslint-disable-next-line no-console
            console.log(`[PREBID_LOG] timeout after ${Date.now() - ms} ms`); // PH_TODO
            done = true;
            resolve();
        }, timeout);
    });
}

async function bla() {
    await blub();
}

function argh() {
    const newDiv = document.createElement("div");
    const newContent = document.createTextNode("Hi there and greetings!");
    newDiv.appendChild(newContent);
    const currentDiv = document.getElementById("start");
    document.body.insertBefore(newDiv, currentDiv);
}
const arghHandle = setInterval(argh, 1);
setTimeout(() => clearTimeout(arghHandle), 2000);

for (let i = 0; i < 1000; i++) {
    bla();
}