JavaScript笔记之 console计时器 time() timeLog() timeEnd()

用法

先取个名字 : let name = “计时器1”; 然后

  1. console.time(name); 计时器起点
  2. 可选 console.timeLog(name); 显示当前耗时
  3. console.timeEnd(name); 显示当前耗时,并结束计时器

测试代码

<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"/><title>time timeLog timeEnd</title>
<style id="FirstStyleTag">

.CodeIB,.CodeB{padding:0px 10px; border-left:10px solid blue; background-color:black; color:gold; font-family:Consolas; white-space:pre;}
.CodeB{display:block} .CodeIB{display:inline-block;}

</style><script>function dgebi(x){if(x && x.constructor===String)x=document.getElementById(x); return x;} ; function dcept(p,tn){var e=document.createElement(tn); p=dgebi(p).appendChild(e); return e;} ; const FirstStyleTag=dgebi("FirstStyleTag");

const 一百万=1000000 , 一千万=一百万*10;

</script></head><body style="margin:0; font-size:16px; "><header id="BodyHeader"><h1>time timeLog timeEnd 的使用</h1></header><div id="BodyBody">
<h3>使用方法: 1.给计时器取个名字. 2.console.time(计时器名字). 3.可选console.timeLog(计时器名字). 4.console.timeEnd(计时器名字).</h3>
<h4>发现: 1.let比var的创建速度快 </h4>

<fieldset class="Example" ><legend></legend><h3>timeLog显示当前耗时,但不结束, timeEnd显示当前耗时,并结束</h3><script>
var name="计时器1";
console.time(name);
var str="";
for(let n=0;n<一百万;++n){
	str+="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	str=str.substring(0,10);
}
console.timeLog(name);
console.timeEnd(name);
</script><code class="CodeIB"></code></fieldset>


<fieldset class="Example" ><legend></legend><h3>timeLog可以不用</h3><script>
var name="计时器2";
console.time(name);
var str="";
for(let n=0;n<一百万;++n){
	str+="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	str=str.substring(0,10);
}
console.timeEnd(name);
</script><code class="CodeIB"></code></fieldset>

<fieldset class="Example" ><legend></legend><h3>timeLog可以不用</h3><script>
var name="计时器3";
console.time(name);
var str="";
for(let n=0;n<一百万;++n){
	var x=Math.random();
}
console.timeEnd(name);
</script><code class="CodeIB"></code></fieldset>

<fieldset class="Example" ><legend></legend><h3>什么也做,光循环</h3><script>
var name="什么也做,循环";
console.time(name);
var str="";
for(let n=0;n<一百万;++n){}
console.timeEnd(name);
</script><code class="CodeIB"></code></fieldset>

<fieldset class="Example" ><legend></legend><h3>var的速度</h3><script>
var name="var的速度";
console.time(name);
var str="";
for(let n=0;n<一百万;++n){var h=3 , i=new String() , j="j" , k=[] , l={} , m=/m/ , a=document.createElement("a");}
console.timeEnd(name);
</script><code class="CodeIB"></code></fieldset>

<fieldset class="Example" ><legend></legend><h3>let的速度</h3><script>
var name="let的速度";
console.time(name);
var str="";
for(let n=0;n<一百万;++n){let h=3 , i=new String() , j="j" , k=[] , l={} , m=/m/ , a=document.createElement("a");}
console.timeEnd(name);
</script><code class="CodeIB"></code></fieldset>


</div><footer id="BodyFooter"></footer><style id="LastStyleTag"></style><script>const BodyHeader=dgebi("BodyHeader") ,    BodyBody=dgebi("BodyBody") ,    BodyFooter=dgebi("BodyFooter") ,   LastStyleTag=dgebi("LastStyleTag") , LastStyleSheet=LastStyleTag.sheet;

var name="计时器1";
console.time(name);
var str="";
for(let n=0;n<10000000;++n){
	str+="a";
	//str=str.substring(0,10);
}
console.timeLog(name);
console.timeEnd(name);

	
function f010(){
	var testCount=0;
	for(let f of document.querySelectorAll(".Example")){
		f.children[0].innerHTML="Example "+(++testCount);
		let script=f.querySelector("script"); let exec=script.innerHTML; let code=f.querySelector("code");
		code.innerHTML=exec; eval(exec);
		setTimeout(()=>{eval(exec);},1);
	}
}
f010();
function f020(){
	console.time('eval在方法内');
	for(let n=0; n<100000;++n)eval("let s=''; s+='a'; let s1=s+'b'; let s2=s1+'c';");
	console.timeEnd('eval在方法内');
}
f020();
console.time('eval在方法外');
var exec020="let s=''; s+='a'; let s1=s+'b'; let s2=s1+'c';"
for(let n=0; n<100000;++n)eval(exec020);
console.timeEnd('eval在方法外');

</script></body></html>

结果截图

JavaScript笔记之 console计时器 time() timeLog() timeEnd()_第1张图片

你可能感兴趣的:(#,Js,JavaScript,ECMAScript,HTML,CSS,JS,js,javascript)