个人用于测试各位javascript爱好者挑战代码性能的页面如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>测试函数性能</title>
</head>
<body>
<div id="debug">
</div>
<script type="text/javascript">
//
function debug(s){
document.getElementById("debug").innerHTML += s;
}
/***
* name:spiderman
* qq:1226359491
* blog:http://blog.csdn.net/FuDesign2008/
*/
function randomColor_fu(){
var color = "000000" + Math.round(Math.random() * 0xffffff).toString(16);
return color.substring(color.length - 6);
}
/****
* name:shelly→水
* qq:49812643
* blog:
*/
function randomColor_shelly(){
return "rgb(" + Math.round(Math.random() * 256) + "," + Math.round(Math.random() * 256) + "," + Math.round(Math.random() * 256) + ")";
}
/****
* name:
* qq:
* blog:
*/
//新增函数
window.onload = function(){
//修改这里以测试不同的函数
var testFn = randomColor_fu;
//修改这里以更改函数执行的次数
var times = 100000;
/*******以下部分不必更改******/
var index;
var d1 = (new Date()).getTime();
for (index = 0; index < times; index++) {
testFn();
}
var d2 = (new Date()).getTime();
debug(testFn.toString() + "<br/>函数执行" + times + "次: " + (d2 - d1) + " ms");
}
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>测试函数性能</title>
</head>
<body>
<div id="debug">
</div>
<script type="text/javascript">
//
function debug(s){
document.getElementById("debug").innerHTML += s;
}
/***
* name:spiderman
* qq:1226359491
* blog:http://blog.csdn.net/FuDesign2008/
*/
function randomColor_fu(){
var color = "000000" + Math.round(Math.random() * 0xffffff).toString(16);
return color.substring(color.length - 6);
}
/****
* name:shelly→水
* qq:49812643
* blog:
*/
function randomColor_shelly(){
return "rgb(" + Math.round(Math.random() * 256) + "," + Math.round(Math.random() * 256) + "," + Math.round(Math.random() * 256) + ")";
}
/****
* name:
* qq:
* blog:
*/
//新增函数
window.onload = function(){
//修改这里以测试不同的函数
var testFn = randomColor_fu;
//修改这里以更改函数执行的次数
var times = 100000;
/*******以下部分不必更改******/
var index;
var d1 = (new Date()).getTime();
for (index = 0; index < times; index++) {
testFn();
}
var d2 = (new Date()).getTime();
debug(testFn.toString() + "<br/>函数执行" + times + "次: " + (d2 - d1) + " ms");
}
</script>
</body>
</html>
有兴趣的朋友可以自己亲自测试一下。
如果你有更好的测试方法,麻烦请告诉我,谢谢!