[Javascript]1. Improve you speed! Loop optimaztion
/**
Improve you loop code
*/
var treasureChest = {
goldCoins: 10000,
magicalItem : "Crown of Speed",
necklaces: ["ruby", "pearl", "sapphire", "diamond"],
openLid: function(){
alert("openLid");
}
}; console.log("You found the following necklaces: ");
//
//**BAD**
//
for(var i = 0; i < treasureChest.necklaces.length; i++){
console.log(treasureChest.necklaces[i]);
}
//The code need to do many things:
//1. the value of i
//2. the treasureChest object
//3. necklaces property
//4. the array pointed to by the property
//5. the length property of the array
//Count steps:
//5 steps * (4 loops + 1 check to stop) = 25 steps /*
What step we can eliminate?
Use "cached values" to curtail lengthy, repetitive to the same data
*/
var x = treasureChest.necklaces.length;
for(var i = 0; i < x; i++){
console.log(treasureChest.necklaces[i]);
}
//Memory access during loop control now only needs to:
//1. retrieve the value of i
//2. retrieve the value of x
//----------- for creating x--------------
//1. ONE TIME COST, creating the variable x in memory
//2-5: the 4 steps finding the value of length
//Count steps:
//2 steps * (4 loops + 1 check to stop) + 5 create extra variable = 15 steps //So we got 25 - 15 = 10 steps less, imaging we have 10000 datas!
//The difference would be:
//5 steps * (10000 loops + 1 check to stop) = 50005 steps
//2 steps * (10000 loops + 1 check to stop) + 5 extra steps for x = 20007 steps
//The difference would be:
//50005 - 20007 = ~30000 !!!! /**
Place your extra control variables inside the loop
*/
for(var i = 0, x = treasureChest.necklaces.length; i < x; i++){
console.log(treasureChest.necklaces[i]);
} /**
Avoid repetitive access at depth
*/
var list = treasureChest.necklaces;
for(var i = 0, x = treasureChest.necklaces.length; i < x; i++){
console.log(list[i]);
} /**
Choose the best loop for arrays,
for-loop usually better than for-in loop
*/
Array.prototype.countType = function(type){
...
}
Array.prototype.removeAll = function(item){
...
}
var list = treasureChest.necklaces;
for(p in list){
console.log(list[i]);
}
//will console out:
//"ruby", "pearl", "sapphire", "diamond", "countType", "removeAll"
//"countType", "removeAll", those are not we want!
//The reason for this is because for-in loop is a property approach to access
//indices will also add in all methods that have been added to the Array prototype.
//prototype makes methods we add becomes Enumerable just like indices.
[Javascript]1. Improve you speed! Loop optimaztion的更多相关文章
- [Javascript]3. Improve you speed! Performance Tips
/** Let inheritance help with memory efficiency */ function SignalFire(ID, startingLogs){ this.fireI ...
- [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 ...
- JavaScript Concurrency model and Event Loop 并发模型和事件循环机制
原文地址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop JavaScript 有一个基于 event loop 的 ...
- 一篇文章图文并茂地带你轻松学完 JavaScript 事件循环机制(event loop)
JavaScript 事件循环机制 (event loop) 本篇文章已经默认你有了基础的 ES6 和 javascript语法 知识. 本篇文章比较细致,如果已经对同步异步,单线程等概念比较熟悉的读 ...
- javascript基础修炼(5)—Event Loop(Node.js)
开发者的javascript造诣取决于对[动态]和[异步]这两个词的理解水平. 一. 一道考察异步知识的面试题 题目是这样的,要求写出下面代码的输出: setTimeout(() => { co ...
- JavaScript并发模型与Event Loop (转载)
并发模型可视化描述 model.svg 如上图所示,Javascript执行引擎的主线程运行的时候,产生堆(heap)和栈(stack),程序中代码依次进入栈中等待执行, 若执行时遇到异步方法,该异步 ...
- javascript的执行机制—Event Loop
既然今天要谈的是javascript的事件循环机制,要理解事件循环,首先要知道事件循环是什么. 我们先从一个例子来看一下javascript的执行顺序. <script> setTimeo ...
- JavaScript 运行机制以及Event Loop(事件循环)
一.JavaScript单线程 众所周知JavaScript是一门单线程语言,也就是说,在同一时间内JS只能做一件事.为什么JavaScript不能有多个线程呢?这样不是能够提高效率吗? JavaSc ...
- JavaScript 的核心机制——event loop(最易懂版)
前言 javascript从诞生之日起就是一门单线程的非阻塞的脚本语言. 非阻塞就是当代码需要进行一项异步任务(无法立刻返回结果,需要花一定时间才能返回的任务,如ajax事件)时,主线程会挂起(pen ...
随机推荐
- 【BZOJ 2721】 2721: [Violet 5]樱花 (筛)
2721: [Violet 5]樱花 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 599 Solved: 354 Description Input ...
- android 数据存储方式
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 1,文件 2,内容提供者 3,偏好设置 4,数据库 5,网络存储. 网络存储,就是上传到 ...
- springboot配置多数据源mongodb
参考大佬的文章 https://juejin.im/entry/5ab304dd51882555825241b3
- Needed Learning(Updating)
决定把掌握不熟练或是模型见的少的知识点在这里列一列 希望能在自己AFO前成功get技能点吧…… 优先级:动态规划-分治-字符串-图论-数据结构-数学-计算几何-其它 动态规划 1.四边形不等式优化 2 ...
- [POJ2337]Catenyms
题目大意: 定义一个catenym是一对单词,满足第一个单词的末尾字符与第二个单词的开头字符相等. 定义复合catenym是一些单词,满足第i个单词的末尾字符与第i+1个单词的开头字符相等. 给你n个 ...
- 配置tomcat让shtml嵌套文件显示
之前,我知道tomcat可以直接解析shtml文件,在浏览器中显示效果来,后来由于需求发生改变,比如说 在做静态化生成的时候一个网站的头部和底部都是一样的,如果每个页面都生成一次,显然很浪费时间,所有 ...
- 提交改动到 github 远程服务器,怎么跳过要求输入密码的步骤
新机器上将工程改动提交到 github 服务器时,发现每次都要输入密码,这个有点儿小烦人,怎么解决这个问题呢? 首先,切换到工程根目录的 .git 隐藏目录,用 TextEdit 打开 config ...
- 【转】教你用C#读写、删除、更新excel表格记录
文章出处:http://blog.csdn.net/kuangshazi515/article/details/6585118 如下图所示,编一个程序,鼠标单击窗体视图区(右边)时,获取一对坐标(X, ...
- 转 iOS获取软键盘的高度
- (void)viewDidLoad { [super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter ...
- tomcat7.0.8的高级应用-apr1.4.2安装
一 windows下安装 直接拷贝tcnative-1.dll到TOMCAT_HOME/bin目录下,启动即可 下载地址 http://archive.apache.org/dist/tomcat/t ...