当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 详解JS中统计函数执行次数与执行时间

详解JS中统计函数执行次数与执行时间

2018年09月24日  | 移动技术网IT编程  | 我要评论

一、统计函数执行次数

常规的方法可以使用 console.log 输出来肉眼计算有多少个输出

不过在chrome中内置了一个 console.count 方法,可以统计一个字符串输出的次数。我们可以利用这个来间接地统计函数的执行次数

function somefunction() {
  console.count('some 已经执行');
}

function otherfunction() {
  console.count('other 已经执行');
}

somefunction(); // some 已经执行: 1
somefunction(); // some 已经执行: 2
otherfunction(); // other 已经执行: 1

console.count(); // default: 1
console.count(); // default: 2

不带参数则为 default 值,否则将会输出该字符串的执行次数,观测起来还是挺方便的

当然,除了输出次数之外,还想获取一个纯粹的次数值,可以用装饰器将函数包装一下,内部使用对象存储调用次数即可

var getfuncalltimes = (function() {
  
  // 装饰器,在当前函数执行前先执行另一个函数
  function decoratorbefore(fn, beforefn) {
    return function() {
      var ret = beforefn.apply(this, arguments);

      // 在前一个函数中判断,不需要执行当前函数
      if (ret !== false) {
        fn.apply(this, arguments);
      }
    };
  }
  
  // 执行次数
  var funtimes = {};
  
  // 给fun添加装饰器,fun执行前将进行计数累加
  return function(fun, funname) {
    // 存储的key值
    funname = funname || fun;
    
    // 不重复绑定,有则返回
    if (funtimes[funname]) {
      return funtimes[funname];
    }
    
    // 绑定
    funtimes[funname] = decoratorbefore(fun, function() {
      // 计数累加
      funtimes[funname].calltimes++;

      console.log('count', funtimes[funname].calltimes);
    });
    
    // 定义函数的值为计数值(初始化)
    funtimes[funname].calltimes = 0;

    return funtimes[funname];
  }
})();
function somefunction() {
  
}

function otherfunction() {
  
}


somefunction = getfuncalltimes(somefunction, 'somefunction');

somefunction(); // count 1
somefunction(); // count 2
somefunction(); // count 3
somefunction(); // count 4

console.log(somefunction.calltimes); // 4



otherfunction = getfuncalltimes(otherfunction);
otherfunction(); // count 1
console.log(otherfunction.calltimes); // 1

otherfunction(); // count 2
console.log(otherfunction.calltimes); // 2

二、统计函数执行时间

chrome中内置了 console.time 和 console.timeend 来打点计算时间

console.time();

for (var i = 0; i < 100000; ++i) {

}

console.timeend(); // default: 1.77197265625ms

不传入参数的话,将以default输出毫秒值

我们可以封装一下,传入函数名称,类似上面的做法,使用装饰器在函数执行前后进行处理

var getfunexectime = (function() {
  
  // 装饰器,在当前函数执行前先执行另一个函数
  function decoratorbefore(fn, beforefn) {
    return function() {
      var ret = beforefn.apply(this, arguments);

      // 在前一个函数中判断,不需要执行当前函数
      if (ret !== false) {
        fn.apply(this, arguments);
      }
    };
  }

  // 装饰器,在当前函数执行后执行另一个函数
  function decoratorafter(fn, afterfn) {
    return function() {
      fn.apply(this, arguments);
      afterfn.apply(this, arguments);
    };
  }
  
  // 执行次数
  var funtimes = {};
  
  // 给fun添加装饰器,fun执行前后计时
  return function(fun, funname) {
    return decoratorafter(decoratorbefore(fun, function() {
      // 执行前
      console.time(funname);
    }), function() {
      // 执行后
      console.timeend(funname);
    });
  }
})();

那么调用的时候,就不需要理会如何计时了

function somefunction() {
  for (var i = 0; i < 100000; ++i) {

  }
}


function otherfunction() {
  for (var i = 0; i < 10000000; ++i) {

  }
}

somefunction = getfunexectime(somefunction, 'somefunction');
somefunction(); // somefunction: 1.616943359375ms

otherfunction = getfunexectime(otherfunction, 'otherfunction');
otherfunction(); // otherfunction: 18.157958984375ms

chrome的console api毕竟不是标准的,除了使用它之外,还可以选择日期插件 date 中的 gettime now 相关方法

然而使用date对象来计算耗时并不正统,推荐使用标准的 performance.now

var start = performance.now();

console.time();

for (var i = 0; i < 10000000; ++i) {

}

var end = performance.now();

console.timeend(); // default: 23.598876953125ms

console.log(end - start); // 23.600000015459955

可以看到,它们是相差不大的

使用类似的方法,将它包装起来以便方便调用

var getfunexectime = (function() {
  
  // 装饰器,在当前函数执行前先执行另一个函数
  function decoratorbefore(fn, beforefn) {
    return function() {
      var ret = beforefn.apply(this, arguments);

      // 在前一个函数中判断,不需要执行当前函数
      if (ret !== false) {
        fn.apply(this, arguments);
      }
    };
  }

  // 装饰器,在当前函数执行后执行另一个函数
  function decoratorafter(fn, afterfn) {
    return function() {
      fn.apply(this, arguments);
      afterfn.apply(this, arguments);
    };
  }
  
  // 执行次数
  var funtimes = {};
  
  // 给fun添加装饰器,fun执行前后计时
  return function(fun, funname) {
    funname = funname || fun;

    if (funtimes[funname]) {
      return funtimes[funname];
    }
    
    // 绑定
    funtimes[funname] = decoratorafter(decoratorbefore(fun, function() {
      // 执行前
      funtimes[funname].timestampstart = performance.now();
    }), function() {
      // 执行后
      funtimes[funname].timestampend = performance.now();
      
      // 将执行耗时存入
      funtimes[funname].valueof = function() {
        return this.timestampend - this.timestampstart;
      };
    });

    return funtimes[funname];
  }
})();
function somefunction() {
  for (var i = 0; i < 100000; ++i) {

  }
}


function otherfunction() {
  for (var i = 0; i < 10000000; ++i) {

  }
}

// 包装
somefunction = getfunexectime(somefunction);
// 执行
somefunction();
// 获取耗时,可直接使用函数的 valueof
console.log(+somefunction); // 2.0999999847263098

otherfunction = getfunexectime(otherfunction, 'otherfunction');
otherfunction(); 
console.log(+otherfunction); // 21.00000000745058

三、如何控制函数的调用次数

也可以通过闭包来控制函数的执行次数

function somefunction() {
  console.log(1);
}

function otherfunction() {
  console.log(2);
}


function setfuncallmaxtimes(fun, times, nextfun) {
  return function() {
    if (times-- > 0) {
      // 执行函数
      return fun.apply(this, arguments);
    } else if (nextfun && typeof nextfun === 'function') {
      // 执行下一个函数
      return nextfun.apply(this, arguments);
    }
  };
}

var fun = setfuncallmaxtimes(somefunction, 3, otherfunction);

fun(); // 1
fun(); // 1
fun(); // 1
fun(); // 2
fun(); // 2

四、如何控制函数的执行时间

因为js是单线程的,控制函数的执行时间相对来说挺麻烦

通过 async await yield 等异步特性,也许还是能办到的

在react 16中的 fiber 机制,在某种意义上是能控制函数的执行时机,得空再去看看它是怎么实现的吧

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网