3 ms·
I would think that "NOP" code like that example would be optimized out: setInterval(() => { var start = Date.now(); while (Date.now() - start < 500) {/* wh
by barrystaes 10y ago
I would think that "NOP" code like that example would be optimized out:
setInterval(() => {
var start = Date.now();
while (Date.now() - start < 500) {/* wheeeee! */}
}, 1000);
- AlphaWeaver 10y agoJavascript doesn't by default do any optimization by removing or modifying code if you don't run the code through some sort of preprocessor or compiler.
- ygra 10y agoOnly it it can prove that Date.now() has no side-effects or that the loop actually ends. (And Date or Date.now() might not be the same things that are usually expected, since anything in JS can change.)
- bzbarsky 10y agoIt's not "NOP" code because it has an observable side-effect, unfortunately: time passing. This can be trivially observed with another interval timer at smaller resolution than 500ms.