[Javascript]1. Improve you speed! Loop optimaztion
2024-08-28 17:34:20
/**
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.
最新文章
- python 面向对象和类成员和异常处理
- 刚看到的感觉会用的到 收藏一下 常用的iOS第三方资源 (转)
- CSS3径向渐变----大鱼吃小鱼之孤单的大鱼
- 【JAVA】Quartz 任务调度和异步执行器
- opencv通过dll调用matlab函数,图片作为参数
- 【BZOJ】【3280】小R的烦恼
- RE:转:一些不常用的html代码
- Oracle系列之表空间
- 关于禁止ViewPager预加载问题【转】
- knob.js进度插件
- Java泛型type体系
- H5中input[type=";date";]默认样式修改 伪类
- webpack简介与使用
- [Leetcode 55]跳格子JumpGame
- golang执行shell命令
- HDU 4548 美素数 素数题解
- openvswitch 源码分析 OVS_ACTION_ATTR_HASH action
- Jquery 筛选选择器
- mvc拦截请求IHttpModule
- FOCUS数据管理:数据字典与多维模型
热门文章
- Git版本管理工具对比(GitBash、EGit、SourceTree)
- hdu 3308 线段树
- HDU 5699 货物运输 二分
- Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 暴力水题
- UIAutomator2.0初始
- linux下mysql自动备份脚本
- Setup Factory打包winform程序
- C#中,为什么在值类型后面加问号