运行时间  Timed Code

批量处理时,如果每次只执行一个任务,显然效率不高,如果能在不影响用户体验和不会卡住页面的前提下,一次执行多个将助于提升体验,运行时间也减少。

运行时间最长为100毫秒,建议50。

运行时间即一开始,一结束,相差即所花时间

var start = +new Date();     +号帮把Date转为数字

var stop;

doSomeThing();

stop = +new Date();

现在就有了优化数组处理模式的代码

function timedProcessArray(items,process,callback) {

  var todo = items.concat();

  setTimeout(function(){

    var start = +new Date();

    do{

    process(todo.shift());

    } while(todo.length > 0 && (+new Date() - start < 50));

    if(todo.length > 0) {

      setTimeout(arguments.callee,25);

    } else {

      callback(items);

    }

  },25);

}

相关文章:

  • 2022-12-23
  • 2021-12-24
  • 2021-08-08
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2021-05-31
  • 2021-04-24
  • 2021-12-11
  • 2021-05-30
  • 2021-07-15
  • 2021-10-20
  • 2022-12-23
相关资源
相似解决方案