简体   繁体   English

如何检查JavaScript每次运行的执行时间?

[英]How to check execution time on every run in JavaScript?

I have this simple function for counting Alphabets frequency. 我有这个简单的函数来计算字母频率。

My code is: 我的代码是:

 function getFreq(str){ var freq={}; str.replace(/[az AZ]/g, function(match){ freq[match] = (freq[match] || 0) + 1; return match; }); console.log(JSON.stringify(freq)); return freq; } var t0 = performance.now(); function doSomething(s){ getFreq(s); }; var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " ms.") 
  Enter Here : <input type="text" onchange="doSomething(this.value);" /> 

I have want to calculate the execution speed of getFreq function after typing some text in the input box. 我想在输入框中输入一些文本后计算getFreq函数的执行速度。 But here dosomething function gets run even without any data in the input box and shows same execution time even after typing some data. 但是,即使在输入框中没有任何数据的情况下, dosomething函数也可以运行,并且即使在键入一些数据后也显示相同的执行时间。

there is api console.time() and console.timeEnd(), u can use this api 有api console.time()和console.timeEnd(),您可以使用此api

for more info - https://developer.mozilla.org/en-US/docs/Web/API/Console/time 有关更多信息-https: //developer.mozilla.org/zh-CN/docs/Web/API/Console/time

see below snippet for example. 请参见下面的代码段。

 function getFreq(str){ var freq={}; str.replace(/[az AZ]/g, function(match){ freq[match] = (freq[match] || 0) + 1; return match; }); console.log(JSON.stringify(freq)); return freq; } function doSomething(s){ console.time("getFreq"); getFreq(s); console.timeEnd("getFreq"); }; 
  Enter Here : <input type="text" onchange="doSomething(this.value);" /> 

  • It was executed before doing anything, because the performances test ( t0 , t1 ) was outside the method and it was never changed. 它在执行任何操作之前就已执行,因为性能测试( t0t1 )在方法之外,并且从未更改过。 Put the measurement inside the method. 将测量结果放入方法中。
  • I also would check the s to be empty before running the method 在运行方法之前,我还将检查s是否为空

 function getFreq(str){ var freq={}; str.replace(/[az AZ]/g, function(match){ freq[match] = (freq[match] || 0) + 1; return match; }); console.log(JSON.stringify(freq)); return freq; } function doSomething(s){ if (s === "") return; var t0 = performance.now(); getFreq(s); var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " ms.") }; 
 Enter Here : <input type="text" onchange="doSomething(this.value);" /> 

console.time() starts a timer with the name concatenation, which is later stopped by console.timeEnd(). console.time()使用名称串联启动一个计时器,该计时器随后由console.timeEnd()停止。 The timer names passed to both function calls have to match in order for the measuring to work. 传递给两个函数调用的计时器名称必须匹配才能使测量工作。

console.time() and console.timeEnd() are only supported by modern browsers, starting with Chrome 2, Firefox 10, Safari 4, and Internet Explorer 11. 从Chrome 2,Firefox 10,Safari 4和Internet Explorer 11开始,现代浏览器仅支持console.time()和console.timeEnd()。

console.time("getFreq");
function getFreq(str){
   var freq={};
  str.replace(/[a-z A-Z]/g, function(match){
  freq[match] = (freq[match] || 0) + 1;
  return match;
  });

 console.log(JSON.stringify(freq));
  return freq;
}
console.timeEnd("getFreq");

This is what I want to do for every onChange call 这就是我要为每个onChange调用所做的

 function getFreq(str){ var t0 = performance.now(); var freq={}; str.replace(/[az AZ]/g, function(match){ freq[match] = (freq[match] || 0) + 1; return match; }); console.log(JSON.stringify(freq)); var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " ms.") return freq; } 
  <input type="text" onchange="getFreq(this.value);" /> 

output : 输出:

{"a":1,"b":1,"c":3," ":2,"A":1,"D":1}
Call to doSomething took 1.4000000000014552 ms.
{"a":1,"b":1,"c":4," ":3,"A":2,"D":1}
Call to doSomething took 1.9250000000029104 ms.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM