Is there a way to pause code execution for just 1ms? The best I can do right now is around 5ms I'm making a sorting visualizer to practice. I started with using divs to represent values of the array that's being sorted but it seemed slow on large arrays so I switched to canvas. But now my choke point is this function I am using to pause the code for just a bit so that the changes on the array are actually visable, otherwise it is so fast that it just sorts at button click. Below is what I used to test the pause function (which I am using in my visualizer), to see if it is actually 1ms because it didn't feel like it.
const number = 100
const delay = 1
const pause = () => new Promise((res) => setTimeout(res, delay));
async function testPause() {
for (let i = 0; i < number; i++){
console.time("pause")
await pause()
console.timeEnd("pause")
}
}
testPause()
It works ok for delay >= 4 . The console displays values of 4 +- 0.5ms. But below 4 it displays same values as for 4. Even when delay = 0. Is there a better way to do this?