/**
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的更多相关文章

  1. [Javascript]3. Improve you speed! Performance Tips

    /** Let inheritance help with memory efficiency */ function SignalFire(ID, startingLogs){ this.fireI ...

  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. JavaScript Concurrency model and Event Loop 并发模型和事件循环机制

    原文地址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop JavaScript 有一个基于 event loop 的 ...

  4. 一篇文章图文并茂地带你轻松学完 JavaScript 事件循环机制(event loop)

    JavaScript 事件循环机制 (event loop) 本篇文章已经默认你有了基础的 ES6 和 javascript语法 知识. 本篇文章比较细致,如果已经对同步异步,单线程等概念比较熟悉的读 ...

  5. javascript基础修炼(5)—Event Loop(Node.js)

    开发者的javascript造诣取决于对[动态]和[异步]这两个词的理解水平. 一. 一道考察异步知识的面试题 题目是这样的,要求写出下面代码的输出: setTimeout(() => { co ...

  6. JavaScript并发模型与Event Loop (转载)

    并发模型可视化描述 model.svg 如上图所示,Javascript执行引擎的主线程运行的时候,产生堆(heap)和栈(stack),程序中代码依次进入栈中等待执行, 若执行时遇到异步方法,该异步 ...

  7. javascript的执行机制—Event Loop

    既然今天要谈的是javascript的事件循环机制,要理解事件循环,首先要知道事件循环是什么. 我们先从一个例子来看一下javascript的执行顺序. <script> setTimeo ...

  8. JavaScript 运行机制以及Event Loop(事件循环)

    一.JavaScript单线程 众所周知JavaScript是一门单线程语言,也就是说,在同一时间内JS只能做一件事.为什么JavaScript不能有多个线程呢?这样不是能够提高效率吗? JavaSc ...

  9. JavaScript 的核心机制——event loop(最易懂版)

    前言 javascript从诞生之日起就是一门单线程的非阻塞的脚本语言. 非阻塞就是当代码需要进行一项异步任务(无法立刻返回结果,需要花一定时间才能返回的任务,如ajax事件)时,主线程会挂起(pen ...

随机推荐

  1. DP Training(Updating)

    感觉前面做了那么多$dp$全是自己想的还是太少啊…… 好像在LZT的博客上看到了不错的资源?赶紧开坑,以一句话题解为主 Codeforces 419B 第一题就开始盗图 由于只有一个交点,手玩一下发现 ...

  2. CVE-2014-4113本地提权测试

    CVE-2014-4113本地提权漏洞分析 By Netfairy 前言 2014年10月14日, Crowdstrike和FireEye发表了一篇文章, 描述了一个新的针对Windows的提权漏洞. ...

  3. bzoj 1069

    最开始想到的是枚举3个点,另一个点用卡壳的思想,但实际上可以只枚举两个点(对角线上的两个点),其余两个点用卡壳. /****************************************** ...

  4. python IDLE 自动提示功能

    \Python27\Lib\idlelib\目录下 config-extensions.def文件修改等待时间 [AutoComplete] enable=1 popupwait=2000(2000表 ...

  5. Notepad++源代码阅读——窗口封装与继承

    引言 近期在看Notepad++的源代码,学习学习Win32 原生API的开发技巧. 本文以Notepad++ 1.0版本的源代码为例讲解如何封装windows窗口,实现面向对象开发,如何通过窗口的继 ...

  6. better-scroll不生效原因

    在vue项目中运用better-scroll插件进行上拉加载的功能时,页面拉不动. html结构: <div class="wrapper" ref="wrappe ...

  7. Git提交空文件夹的技巧

    这个只能说是技巧不能说是方法,原理是在每个空文件夹新建一个.gitignore文件,然后提交. 快捷命令: find . -type d -empty -exec touch {}/.gitignor ...

  8. 新型穿墙监控雷达Range-R:让你的隐私无所遁形(转)

    还是隐私问题,原帖地址:http://www.freebuf.com/news/57446.html 在我们的认知中,政府对民众的监控已经成为一种常态.从电话.电子邮件到通信聊天.社交网络,一切细节都 ...

  9. HDU 4730 We Love MOE Girls (2013成都网络赛,签到水题)

    We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. Setup Factory打包winform程序

    摘要 Setup Factory是一款软件安装工具.Setup Factory支持创建一个安装文件或一个单间的setup.exe文件,生成文件可以运行于任意版本的windows中. 步骤 1.安装Se ...