/**
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. 关于SetCapture() 和 ReleaseCapture()的使用方法

    查MSND,对SetCapture()函数的说明为:“该函数在属于当前线程的指定窗体里设置鼠标捕获.一旦窗体捕获了鼠标,全部鼠标输入都针对该窗体,不管光标是否在窗体的边界内.同一时刻仅仅能有一个窗体捕 ...

  2. VCS仿真 Dump Memory

    VCS仿真 Dump Memory 两种方法 vcs联合verdi生成fsdb文件 vcs生成vpd文件 VCS联合verdi生成fsdb文件 1.testbench中加入如下语句: initial ...

  3. HTML与CSS入门——第十三章  使用框架

    知识点: 1.建立框架集的方法 2.在框架和窗口之间链接的方法 3.使用内联框架的方法 13.1 什么是框架: 框架是浏览器窗口中的一个矩形区域,每个框架显示的是一个完整的页面. 作者不建议使用框架, ...

  4. DIV+CSS 自适应布局

    两栏布局,左边定宽200px,右边自适应.如何实现?我的第一个反应就是:用flex伸缩盒呀,然后balabala...说完之后,面试官说,还有没有别的方法?flex有些浏览器不支持嗯...我愣了一下, ...

  5. 《第一行代码》学习笔记9-活动Activity(7)

    1.发现Android中的活动是可以层叠的,每启动一个新的活动,就会覆盖在原活动之上, 然后点击Back键会销毁最上面的活动,下面的一个活动就会重新显示出来. 2.Android是使用任务来管理活动的 ...

  6. web并发访问的问题

    一般的webapplication,可能会遇到这样的问题,你可以这样模拟:用浏览器开一个窗口,选中一条记录,编辑之,但是先不要保存,新开一个浏览器窗口,找到这条记录,删除之,然后再回到第一个窗口点击保 ...

  7. va_list/va_start/va_arg/va_end深入分析

    http://www.cnblogs.com/justinzhang/archive/2011/09/29/2195969.html

  8. OpenGL ES 2.0 绘制方式

    OpenGL ES 中支持的绘制方式大致分3类,包括点.线段.三角形,每类中包括一种或多种具体的绘制方式. GL_POINTS 传入渲染管线的一系列顶点单独进行绘制. GL_LINES   传入渲染管 ...

  9. 医院设置 codevs 2577

    Floyd+二叉树 #include<iostream>#include<cstdlib>#include<cstdio>#include<cstring&g ...

  10. sublime快捷键保存

    快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...