пятница, 12 сентября 2014 г.

Тестирование производительности в Js

Грубый метод измерения времени выполнения блока кода
console.time("Id");
//блок кода
console.timeEnd("Id");

Более точный:

function SpeedTest(testImplement, testParams, repetitions){
  this.testImplement = testImplement;
  this.testParams = testParams;
  this.repetitions = repetitions || 10000;
  this.average = 0;
}
SpeedTest.prototype = {
  startTest: function(){
    var beginTime, endTime, sumTimes = 0;
    for (var i = 0, x = this.repetitions; i < x; i++){
      beginTime = +new Date();
      this.testImplement( this.testParams );
      endTime = +new Date();
      sumTimes += endTime - beginTime;
    }
    this.average = sumTimes / this.repetitions;
    return console.log("Average execution across " +
                        this.repetitions + ": " +
                        this.average);   
  }
}

Комментариев нет:

Отправить комментарий