/**
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. ORACLE查看数据文件-控制文件-日志文件-表空间信息

    1.查看当前数据库中的所有用户:select username from dba_users; 2.查看当前会话登录的用户:show user或select username from user_us ...

  2. VMware SphereESXi上传系统镜像

    VMware SphereESXi上传系统镜像 打开右侧[摘要]选项卡 在[资源]中选择存储器中的存储,右键[浏览数据库存储] 选择工具栏[创建文件夹]图标,命名后保存 这样随后找到存储设备,浏览刚才 ...

  3. Oracle System密码忘记 密码修改、删除账号锁定lock

    一下转自http://www.cnblogs.com/yjhrem/articles/2340149.html 运行cmd命令行 录入 sqlplus /nolog  无用户名登录 conn /as ...

  4. 再入门JavaScript

    从去年毕业到现今,工作不到一年.接触了3个实际项目,一个实训项目.却反而只有实训项目做的比较像样子. 重新又回到写脚本的岗位上,第一次真正意义上接触脚本应该是在达内培训时候李大神所引进,大神各种技术, ...

  5. (转+原)python中的浅拷贝和深拷贝

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069722.html 原网址: http://blog.csdn.net/sunshine_in_mo ...

  6. windows下自动删除n天前的文件

    使用windows2003下的内置命令forfiles配合计划任务可以实现自动删除n天前的文件. windows2003中设定自动执行的计划任务很简单. 一.脚本编写 forfiles命令用法: Fo ...

  7. 怎样利用putty登陆SSH主机方法

    PuTTY 是一套免费的SSH / Telnet 程序,是在Windows 32平台下的telnet.rlogin和ssh客户端,它是一个跨平台的远程登录工具 下载putty成功后,双击打开Putty ...

  8. git:hook declined FATAL: W refs/heads DENIED by fallthru error

    hook declined FATAL: W refs/heads DENIED by fallthru error git提交代码时报错,网上查了,最终结果竟然是测试人员没有给我配置写的权限,配置了 ...

  9. 1169 二叉树遍历(XCOJ DFS)

    给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 样例输入 BADC BDCA 样例输出 ABCD #include <iostream> ...

  10. DataTables获取表单输入框数据

    $(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...