通常我們需要對(duì)函數(shù)進(jìn)行優(yōu)化,一般的做法是開(kāi)始的時(shí)候獲得時(shí)間,結(jié)束的時(shí)候再獲得一次時(shí)間,兩次時(shí)間相減就能到到花費(fèi)的時(shí)間。而函數(shù)運(yùn)行速度之快,基本上都是毫秒級(jí)的。
下面給出的函數(shù)就是就此準(zhǔn)備的。
// 時(shí)間轉(zhuǎn)為時(shí)間戳(毫秒)
function time2stamp(){
var d = new Date();
return Date.parse(d)+d.getMilliseconds();
}
用法:
var t1 = time2stamp();
// 比較各游覽器的DOM運(yùn)行速度。
var divs = document.getElementByTagName("div");
var t2 = time2stamp();
alert("耗時(shí):" + (t2 - t1) + " 毫秒");
新的方法:
var time1 = new Date
// 比較各游覽器的DOM運(yùn)行速度。
var divs = document.getElementByTagName("div");
alert("耗時(shí):" + (new Date - time1) + " 毫秒");
測(cè)試各種循環(huán)的效率,for...in這樣慢的循環(huán)就不測(cè)了!
!doctype html>
html dir="ltr" lang="zh-CN">
head>
meta charset="utf-8"/>
title>類/title>
script type="text/javascript" charset="utf-8">
window.onload = function(){
for (var i=0, elm; i4000; i++) {
elm = document.createElement("div");
elm.innerHTML = "第一重divdiv>第二重divdiv>第三重div/div>/div>";
document.body.appendChild(elm);
}
document.getElementById("start-test").onclick = startTest;
var uuid = 0;
function startTest() {
var divs = document.getElementsByTagName("div");
var time1 = new Date();
var i = -1, n = divs.length;while (++i n) {
divs[i].uuid = uuid++;
}
document.getElementById("time-1").innerHTML = new Date() - time1;
var time2 = new Date();
var i = divs.length; while (--i >= 0) {
divs[i].uuid = uuid++;
}
document.getElementById("time-2").innerHTML = new Date() - time2;
var time3 = new Date();
for (var i=0, n=divs.length; in; i++) {
divs[i].uuid = uuid++;
}
document.getElementById("time-3").innerHTML = new Date() - time3;
var time4 = new Date();
for (var n = divs.length - 1; n >= 0; n--) {
divs[n].uuid = uuid++;
}
document.getElementById("time-4").innerHTML = new Date() - time4;
var time5 = new Date();
for (var n = divs.length - 1; n--;) {
divs[n].uuid = uuid++;
}
document.getElementById("time-5").innerHTML = new Date() - time5;
var time6 = new Date();
var n = divs.length-1; do {
divs[n].uuid = uuid++;
} while (n--);
document.getElementById("time-6").innerHTML = new Date() - time6;
var time7 = new Date();
for(var i=0,el;el = divs[i++];){
el.uuid = uuid++;
};
document.getElementById("time-7").innerHTML = new Date() - time7;
}
}
/script>
/head>
body>
style>
h2 {
margin:0;
padding:10px;
font-weight:500;
font-size:13px;
background:#8080C0;
color:#fff;
}
p {
margin:0;
text-indent:2em;
border-bottom:2px solid #a9ea00;
background:#F2F1D7;
}
span.red{
color:red;
}
/style>
h1>JavaScript loop speed test/h1>
ul>
li>遍歷頁(yè)面上12000個(gè)DIV/li>
/ul>
input id="start-test" type="button" value="Start test">
h2>循環(huán)1: var i = -1, n = divs.length;while (++i n) { }span class="red">順序/span>/h2>
p id="time-1">/p>
h2>循環(huán)2:var n = divs.length; while (--n >= 0) { }span class="red">倒序/span>/h2>
p id="time-2">/p>
h2>循環(huán)3:for (var i=0, n=divs.length; in; i++)span class="red">順序/span>/h2>
p id="time-3">/p>
h2>循環(huán)4:for (var n = divs.length - 1; n >= 0; n--) { }span class="red">倒序/span>/h2>
p id="time-4">/p>
h2>循環(huán)5:for (var n = divs.length - 1; n--;) { }span class="red">倒序/span>/h2>
p id="time-5">/p>
h2>循環(huán)6: var n = divs.length-1; do { } while (n--)span class="red">倒序/span>/h2>
p id="time-6">/p>
h2>循環(huán)7: for(var i=0,el;el = divs[i++];){ } span class="red">順序/span>/h2>
p id="time-7">/p>
/body>
/html>
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:- Js 時(shí)間間隔計(jì)算的函數(shù)(間隔天數(shù))
- 用js計(jì)算頁(yè)面執(zhí)行時(shí)間的函數(shù)
- JavaScript格式化日期時(shí)間的方法和自定義格式化函數(shù)示例
- javascript時(shí)間函數(shù)大全
- js查看一個(gè)函數(shù)的執(zhí)行時(shí)間實(shí)例代碼
- JavaScript日期時(shí)間格式化函數(shù)分享