/**
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. hdu 4859 最小割

    链接:点我 未懂

  2. SQL Server中执行正则表达式

    总体方案:写function,再执行update语句. 一.查询函数 -- ============================================= -- Author: <l ...

  3. bzoj 3282

    回顾一下LCT,容易写错的地方: 1.每次断掉Splay中的边,必须update一下父亲节点,再根据具体情况是否splay父亲节点. 2.养成没有用的值(比如当pre[u]不为0时的pnt[u])不去 ...

  4. python开发_函数的参数传递

    在这个用例中,我们要讨论的是关于函数的传参问题 我所使用的python版本为3.3.2 对于函数: def fun(arg): print(arg) def main(): fun('hello,Ho ...

  5. python 爬虫 处理超级课程表传输的数据

    借鉴的别人的思路 http://www.oschina.net/code/snippet_2463131_53711 抓取超级课程表传输的数据 他的传输数据居然是明文的-- 现在已经把自己的课表都抓出 ...

  6. Jmeter实现登录、创建BUG、解决bug的手写脚本

    一.登录 1.          打开jmeter.添加线程组,命名为test,如下图: 2.          添加HTTp默认请求 1)服务器名称或IP:这里只能填写域名或IP地址 2)端口号:配 ...

  7. PostgreSQL增强版命令行客户端(pgcli)

    效果: 安装: https://www.pgcli.com/install 官网: https://www.pgcli.com/

  8. Automatic WordPress Updates Using FTP/FTPS or SSH

    Introduction When working with WordPress in a more secure environment where websites are not entirel ...

  9. SQL Server 中添加表注释

    今天在创建完表之后,发现没有办法给表添加注释说明,字段的注释可以在建表的时候就添加,上网查了一下使用SQL给表添加注释的方法,方法如下: -- 表加注释 EXEC sys.sp_addextended ...

  10. “CObject::operator =”: 无法访问 private 成员(在“CObject”类中声明)

    c++工程编译报错: “CObject::operator =”: 无法访问 private 成员(在“CObject”类中声明) 错误无法直接定位源码位置,网上搜索了,也和我的代码不一样. 最后还是 ...