04月18, 2014

一个测试 dom 和 $的小东东

一个测试dom和$的小东东, 只为留着, 不为别的

//DOM是已知的
var J_top_bar = document.createElement("div");
J_top_bar.style.cssText = "position:fixed;left:100px;top:100px;background-color:#fff;color:#000;padding:10px;border:2px solid #09f;";
document.body.appendChild(J_top_bar);
J_top_bar.setAttribute("data-xl", 1);
J_top_bar.innerHTML = "1";
//js
function a (argument) {
 //不用模拟, 困为
 var that = this;
 if(!that._mark){
 that._mark = 1;
 //操作dom
 that.innerHTML = that.innerHTML * 1 + 1;
 that.setAttribute("xl", that.getAttribute("xl") * 1 + 1);
 }
 delete that._mark;//去掉标识
}
//$
function b (argument) {
 //模拟每一次的点击, 因为 点击里面要用 $this = this;
 var $this = jQuery(this);
 if(!$this.data("mark")){
 $this.data("mark");
 //操作dom
 $this.html(function(i, val){
 return val * 1 + 1;
 }).attr("data-xl", $this.attr("data-xl") * 1 + 1);
 }
 $this.data("mark", 0);//去掉标识
}
var start = new Date - 0;
// console.time("dom");
for(var i = 0;i<10000;i++){
 a.call(J_top_bar);
}
// console.timeEnd("dom");
alert("dom : "+ ((new Date - 0) - start))
start = new Date - 0;
// console.time("$");
for(var i = 0;i<10000;i++){
 b.call(J_top_bar);
}
// console.timeEnd("$");
alert("$ : "+ ((new Date - 0) - start))

本文链接:https://xuexb.com/post/196.html

-- EOF --

发表于 2014年04月18日 16:24:49 ,添加在分类 前端技术 下 ,最后修改于 2016年07月16日 16:24:49

提醒: 本文最后更新于 3466 天前,文中所描述的信息可能已发生改变,请谨慎使用。

Comments

评论加载中...

注:如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理。

AltStyle によって変換されたページ (->オリジナル) /