/**
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. 6、Cocos2dx 3.0游戏开发找小三之游戏的基本概念

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27689713 郝萌主友情提示: 人是习惯的产物,当你 ...

  2. 初探HTML

    就在今天我抱着试一试的态度稍微看了下HTML5, 尝试着接触一点新知识, 虽然学的并不多, 但是还是异常的兴奋, 感觉有好多东西和之前的不一样了, 包括控件的创建和一些属性的设置等, 总之这些变化与改 ...

  3. 关于C语言中的inline

    在c中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间是有限的 ...

  4. 远程连接到Fedora

    首先执行以下3点(主要是前两点) 第一: 开启ssh #service sshd restart 第二:关闭防火墙 #service iptables stop 第三:selinux(重启电脑后失效) ...

  5. easyUI的datagrid控件日期列不能正确显示Json格式数据的解决方案

    EasyUI是一套比较轻巧易用的Jquery控件,在使用过程中遇到一个问题,它的列表控件——datagrid, 在显示日期列的时候,由于后台返回给页面的数据是Json格式的,其中的日期字段,在后台是正 ...

  6. 解决linux下oracle进入sqlplus环境中后退键显示^H、上下键无效与ctrl+l无法清屏等问题【weber出品必属精品】

    习惯linux中上下键回退历史输入的人们肯定也希望sqlplus中也能实现相同的效果,可是不幸的是,sqlplus中不提供诸多方便的快捷键,这个时候我们就需要另外安装一个插件来实现这个想法. 这个插件 ...

  7. LAMP架构搭建+Discuz论坛搭建【weber出品必属精品】

    一.     本机简介: 本机系统: CentOS-6.4-x86_64 主机名:oracle.ywb IP地址:192.168.146.129 二.     在Linux环境下安装Apache步骤 ...

  8. HTTP get、post请求

    Post请求: string postData = "user=123&pass=456"; // 要发放的数据 byte[] byteArray = Encoding.U ...

  9. 判别linux机器字节序为大端还是小端

    代码如下: #include <iostream> #include <arpa/inet.h> #include <cstdio> using namespace ...

  10. CCFileUtils::getFileData疑惑

    背景 这几天在使用cocos2d-x读取磁盘文件的时候,发现了CCFileUtils中一点不合理的地方,特此记录,以供研讨. 项目结构 ①我使用的是cocos2d-x 2.1.3版本,CCFileUt ...