admin 管理员组

文章数量: 1086019

I have this example that pares the performance of a simple counter loop using for loop and using setInerval, the exeuction time difference is huge as follows:

var i = 0;
var i2 = 0;
var int1 = null;

console.time("for loop");
do{  
  i++;
}while(i <= 1000);
console.timeEnd("for loop"); 


function fnc(){
  if(i2++ == 1000){
    clearInterval(int1);
    console.timeEnd("interval loop");
  }
}

console.time("interval loop");
int1 = setInterval(fnc , 1);

Outputs

See demo at: ,output

I would like to know if it is possible to execute a function using setInterval with a timespan less than 1ms? or is there a way to achieve better performance for the given example using setInterval?

I have this example that pares the performance of a simple counter loop using for loop and using setInerval, the exeuction time difference is huge as follows:

var i = 0;
var i2 = 0;
var int1 = null;

console.time("for loop");
do{  
  i++;
}while(i <= 1000);
console.timeEnd("for loop"); 


function fnc(){
  if(i2++ == 1000){
    clearInterval(int1);
    console.timeEnd("interval loop");
  }
}

console.time("interval loop");
int1 = setInterval(fnc , 1);

Outputs

See demo at: http://jsbin./jusiqilayi/edit?js,output

I would like to know if it is possible to execute a function using setInterval with a timespan less than 1ms? or is there a way to achieve better performance for the given example using setInterval?

Share Improve this question asked Jul 3, 2016 at 4:55 Shadi ShaabanShadi Shaaban 1,7201 gold badge11 silver badges17 bronze badges 6
  • I've been looking at your question for more than 5 minutes now and I just don't understand why would someone want to use the setInterval method in this example? or maybe I just don't get the question?! – Amin Jafari Commented Jul 3, 2016 at 5:05
  • The question is about the bottom line of setInerval performance, is it possible to get a better execution time for simple 1 instruction code as simple as increasing a variable? hope it's more clear now. – Shadi Shaaban Commented Jul 3, 2016 at 5:10
  • as @Ismail answered below the minimum delay is 4ms (you can try doubling your condition value to see the effect) also the whole point of using interval is to create a delay before each call, using it without a delay logically wouldn't make sense. – Amin Jafari Commented Jul 3, 2016 at 5:15
  • Yes, omitting the delay is not the point, however what about using 0.5ms or 0.01ms as delay? maybe it is possible to achieve better performance than the 4ms stated in @Ismail answer – Shadi Shaaban Commented Jul 3, 2016 at 5:20
  • again that wouldn't make sense 'cause that's about as long as it'd take to normally run any function – Amin Jafari Commented Jul 3, 2016 at 6:13
 |  Show 1 more ment

1 Answer 1

Reset to default 9

setInterval and setTimeout are forced to use at least the minimum delay. The minimum delay, DOM_MIN_TIMEOUT_VALUE, is 4 ms which is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward.

In modern browsers you can use window.postMessage() as a workaround to implement a 0 ms timeout as described here.

本文标签: Javascript setInterval execution in less than 1 msStack Overflow