/**
Let inheritance help with memory efficiency
*/
function SignalFire(ID, startingLogs){
this.fireID = ID;
this.logsLeft = startingLogs;
} SignalFire.prototype = {
addLogs: function(numLogs){
this.logsLeft += numLogs;
} lightFire: function(){
alert("Whooosh!");
} smokeSignal: function(message){
if(this.logsLeft < this.message.length / 10){
alert("Not enough");
}else{
this.lightFire();
var x = this.message.length;
for(var i = 0; i < x; i++){
alert("((("+this.message[i]+")))");
if(i % 10 == 0 && i != 0){
this.logsLeft--;
}
}
}
}
} var fireOne = new SignalFire(1, 20);
var fireTwo = new SignalFire(2, 18);
var fireThree = new SignalFire(3, 14); fireOne.addLogs(8);
fireTwo.addLogs(10);
fireThree.addLogs(14);
fireThree.smokeSignal("Goblins!");
//The addLogs method is found in one useful place, instead of being
//replicated across all signalFires. /**
Adding individual dom elements ain't always speedy
*/
//
//**BAD**
//
var list = document.getElementById("kotwList");
var kotw = ["Jenna Rangespike",
"Neric Farthing",
"Darkin Stonefield"];
for(var i = 0, x = kotw.length; i < x; i++){
var element = document.creatElement('li');
element.appendChild(document.createTextNode(kotw[i]));
list.appendChild(element);
}
//appendChild(); function each time this list is appened, we access the DOM
//and cause an entire document reflow. Not as speedy as we'd like, especially
//if we list was huge... /*
Improve: Use a document fragment to insert additions all at once
*/
var list = document.getElementById("kotwList");
var kotw = ["Jenna Rangespike",
"Neric Farthing",
"Darkin Stonefield"];
var fragment = document.createDocumentFragment();
for(var i = 0, x = kotw.length; i < x; i++){
var element = document.creatElement('li');
element.appendChild(document.createTextNode(kotw[i]));
fragment.appendChild(element);
}
list.appendChild(fragment); /*
Improve: declare variables as few times as possible
*/
//
//**GOOD**
//
var list = document.getElementById("kotwList"),
kotw = ["Jenna Rangespike",
"Neric Farthing",
"Darkin Stonefield"],
fragment = document.createDocumentFragment(),
element; for(var i = 0, x = kotw.length; i < x; i++){
element = document.creatElement('li');
element.appendChild(document.createTextNode(kotw[i]));
fragment.appendChild(element);
}
list.appendChild(fragment); /**
Efficient choices for string concatenation
*/
//
//**OK**
//
var knight = "Jenna Rangespike",
action = " strikes the dragon with a ",
weapon = "Halberd",
turn = "";
turn += knight;
turn += action;
turn += weapon;
//It is ok because the code is short and clear //
//**NOT GOOD ENOGUTH
//
var newPageBuild = ["<!DOCTYPE html>", "<html>", "<body>", "<h1>",
*** a hundred or more other html elements***,
"</script>", "</body>", "</html>"],
page = "";
for(var i = 0, x = newPageBuild.length; i < x; i++){
page += newPageBuild[i];
}
//
//**GOOD**
//
page = newPageBuild.join("\n");
//Using join is much faster than using '+=' and looks simple and clear!!

[Javascript]3. Improve you speed! Performance Tips的更多相关文章

  1. [Javascript]1. Improve you speed! Loop optimaztion

    /** Improve you loop code */ var treasureChest = { goldCoins: 10000, magicalItem : "Crown of Sp ...

  2. [Javascript]2. Improve you speed! Script Execution

    Let's take a closer look at how a browser retrieves and acts on scripts.modern browser can parallel ...

  3. Android 性能优化(19)*代码优化11条技巧:Performance Tips

    Performance Tips 1.In this document Avoid Creating Unnecessary Objects 避免多余的对象 Prefer Static Over Vi ...

  4. 转载:Why using Single Root I/O Virtualization (SR-IOV) can help improve I/O performance and Reduce Costs

    Introduction While server virtualization is being widely deployed in an effort to reduce costs and o ...

  5. SQL Server performance tips

    Refer to: http://harriyott.com/2006/01/sql-server-performance-tips A colleague of mine has been look ...

  6. How To Improve Deep Learning Performance

    如何提高深度学习性能 20 Tips, Tricks and Techniques That You Can Use ToFight Overfitting and Get Better Genera ...

  7. 翻译--Blazing fast node.js: 10 performance tips from LinkedIn Mobile

    1.避免使用同步代码: // Good: write files asynchronously fs.writeFile('message.txt', 'Hello Node', function ( ...

  8. Performance tips

    HTML5 Techniques for Optimizing Mobile Performance Scrolling Performance layout-performance

  9. Json.NET Performance Tips

    原文: http://www.newtonsoft.com/json/help/html/Performance.htm To keep an application consistently fas ...

随机推荐

  1. [RxJS] Getting Input Text with Map

    By default, Inputs will push input events into the stream. This lesson shows you how to use map to c ...

  2. Hadoop动态加入/删除节点(datanode和tacktracker)

    大体,正确的做法是首选的配置文件,然后开始详细机对应的进程/停止操作. 网上一些资料说在调整配置文件的时候,优先使用主机名而不是IP进行配置. 总的来说加入/删除DataNode和TaskTracke ...

  3. 用SNMP协议实现系统信息监控--CentOS

    (1)  安装SNMP客户端以及服务端 安装内容为三项:net-snmp  net-snmp-devel   net-snmp-utils 安装命令:yum install net-snmp net- ...

  4. Java------------运算符优先级速记口诀

    单目乘加位关系,逻辑三目后赋值. 单目:单目运算符+ –(负数) ++ -- 等 乘加(乘除加减):算数单目运算符* / % + - 位:位移单目运算符<< >> 关系:关系单 ...

  5. iOS~~MD5加密

    // 一般加密 +(NSString *)md5String:(NSString *)str { const char *password=[str UTF8String]; unsigned cha ...

  6. MFC多线程编的可能

    1. 之所以是“可能”,因为这里有个重点就是临时对象是HWND操作的封装,不是窗口类的封装.因此所有的HWND临时对象都是CWnd的实例,即使上面强行转换为CAbcDialog*也依旧是CWnd*,所 ...

  7. ci 的控制器文件夹下开加子文件夹

    在一个比较大的项目中,希望controllers下再细分子文件夹.例如:controllers/pj,controllers/xxk等. 做法是: 1.在controllers下添加相关的子文件夹,例 ...

  8. 移动WEB开发常用技巧

    Meta设置 <!-- 设备宽度.禁止缩放 --> <meta name="viewport" content="width=device-width, ...

  9. 求实现sql?

    id name pid1 曾祖父 02 祖父 13 父亲 24 儿子 35 孙子 4备注:用一条数据库语句来解决查询结果:name1 name2 name3曾祖父 祖父 父亲曾祖父 父亲 儿子曾祖父 ...

  10. hdu 1595 find the longest of the shortest

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...