0

Let's say I am forced to sleep in this barbaric way.

I cannot use events (such as setTimeout), it isn't in the constraints of the problem and doing such a thing has already been previously discussed in depth.

function sleep(milliseconds) {
   var start = new Date().getTime();

   while (new Date().getTime() < start + milliseconds){
        // need some NOP here
   }
}

All I wanna know is, can you think of something I could stick in there? Preferably a single statement that takes a long time to run (maybe that takes time to malloc a huge region?)

Taking a ton of CPU time is fine, even though it isn't technically a NOP. Additionally, oversleeping is fine. Freezing the browser is not a concern. I am most interested in minimizing the amount of statements that run during the sleep duration, so basically: the longest-executing single statement that doesn't really "do" anything.

Ziggy
  • 95
  • 1
  • 5
  • 2
    Why would you need anything in there, the loop is proving the load, any other code will run at just the same rate. – Blindman67 Apr 13 '17 at 03:06
  • http://stackoverflow.com/questions/33383840/is-there-a-javascript-equivalent-for-the-python-pass-statement-that-does-nothing – jrook Apr 13 '17 at 03:09
  • 1
    *"Preferably a single statement that takes a long time to run"* - What if it took longer to run than the specified number of `milliseconds`? Your function would sleep for too long. Why would you be "forced" to do this? – nnnnnn Apr 13 '17 at 03:15
  • 4
    Would it be possible for you to provide an example of why anyone would actually want to do this? – Shadow Apr 13 '17 at 03:21
  • I don't think so. Any synchronous operation in a loop will be using CPU heavily. Event if there would be `NOP` operation, it still runs in a loop, millions times a second. – pumbo Apr 13 '17 at 03:22
  • 1
    This looks like a case of a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me... – Andreas Apr 13 '17 at 03:41
  • If it takes a long time, then it's not a NO-OP. A NO-OP would be `;` (or nothing at all inside the `{}`). You do understand that whatever kind of long-running operation or loop you do, the browser will freeze while it is running, right? –  Apr 13 '17 at 04:44
  • Taking a ton of CPU time is fine, even though it isn't technically a NOP. Additionally, oversleeping is fine. Freezing the browser is not a concern. I am most interested in minimizing the amount of statements that run, so basically: the longest-executing single statement that doesn't really "do" anything. – Ziggy Apr 13 '17 at 05:40
  • You already have a loop. You don't need anything else. It already works the way you described. – pumbo Apr 13 '17 at 06:06

0 Answers0