对空位的处理

for循环(不会忽略空位,标记undefined)

var arr =[1,2,undefined,3,null,,7]
for (let i=0;i<arr.length;i++) {
console.log('for循环',arr[i])
} 结果:
for循环 1
for循环 2
for循环 undefined
for循环 3
for循环 null
for循环 undefined
for循环 7

for of(不会忽略空位,标记undefined)

for(let i of arr) {
console.log('for of',i)
} 结果:
for of 1
for of 2
for of undefined
for of 3
for of null
for of undefined
for of 7

for in(会忽略空位)

for(let i in arr) {
console.log('for in',arr[i])
} 结果:
for in 1
for in 2
for in undefined
for in 3
for in null
for in 7

forEach(会忽略空位)

arr.forEach(item => console.log('foreach',item))

结果:
foreach 1
foreach 2
foreach undefined
foreach 3
foreach null
foreach 7

map(会忽略空位),filter,every,some,find,findIndex都会忽略空位

arr.map(item => console.log('map',item))

结果:
map 1
map 2
map undefined
map 3
map null
map 7

性能对比

var arr =new Array(100000).fill(1)
console.time('for循环')
for (let i=0;i<arr.length;i++) {
}
console.timeEnd('for循环') console.time('缓存优化的for循环')
for (let i=0,len=arr.length;i<len;i++) {
}
console.timeEnd('缓存优化的for循环') console.time('foreach循环')
arr.forEach(item => {} )
console.timeEnd('foreach循环') console.time('for of 循环')
for(let i of arr) {
}
console.timeEnd('for of 循环') console.time('for in 循环')
for(let i in arr) {
}
console.timeEnd('for in 循环') console.time('map循环')
arr.map(item => {})
console.timeEnd('map循环') 结果: for循环: 3.226ms
缓存优化的for循环: 1.965ms
foreach循环: 2.144ms
for of 循环: 5.128ms
for in 循环: 11.501ms(最好不要用,可能会遍历原型链上的属性)
map循环: 13.134ms

注意lz在对数组的循环中没有做任何处理仅仅是空代码来比较性能,map循环直接返回空数组,在对数组进行浅拷贝上占用内存低,for of循环也对占用内存上进行了一定的优化,而for与for of可以随时中断循环与跳出循环。抛开业务场景和使用便利性,单纯谈性能和效率是没有意义的

ES6新增的诸多数组的方法确实极大的方便了前端开发,使得以往复杂或者冗长的代码,可以变得易读而且精炼,而好的for循环写法,在大数据量的情况下,确实也有着更好的兼容和多环境运行表现

js中循环对比(for循环,foreach,for in,for of ,map)的更多相关文章

  1. 一文搞懂 js 中的各种 for 循环的不同之处

    一文搞懂 js 中的各种 for 循环的不同之处 See the Pen for...in vs for...of by xgqfrms (@xgqfrms) on CodePen. for &quo ...

  2. js中的分支与循环

    一.js的分支结构 js的分支结构包括:if-else结构.多重if结构.嵌套if结构和switch-case结构 1.if-else结构 1.结构的写法:    if(判断条件){    //条件为 ...

  3. js中奇特的for循环写法

    //正常的for循环 for(var i=0;i<10;i++){ console.log(i); } //输出:1,2,3……10 //简写 for(var i=10;i--;){ conso ...

  4. JS中的for....in循环 和 for ...of循环以及iterable遍历Map和Set

    for循环的一个变体是for ... in循环,它可以把一个对象的所有属性依次循环出来: var o = { name: 'Jack', age: 20, city: 'Beijing' }; for ...

  5. js中数组以及for循环的使用

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  6. js 中的for...in循环

    in:其左边是一个字符串或可以转换成字符串,右边是一个对象或数组 例:var person={firstname:"Bob", lastname:"Kin"}; ...

  7. JS中的三种循环

    三种循环1.while 2.do while 3.for 1.while: 语法结构:while(条件){代码块:改变条件} 步骤:1.初始化变量   2.判断条件  3.执行代码块  4.改变初始条 ...

  8. js中两种for循环的使用

    针对两种for循环的使用 1. for in循环的使用环境     可用在字符串.数组.对象中, 需注意:其中遍历对象得到的是每个key  的value值  2. for 变量递加的方式        ...

  9. js 中的流程控制-循环(for)语句

    for语句: <script> /* for(exp1;exp2;exp3){ 循环体; } exp1:无条件的执行第一个表达式 exp2:判断是否能执行循环体的条伯 exp3:做增量的操 ...

随机推荐

  1. 软件工程(FZU2015) 赛季得分榜,第四回合

    SE_FZU目录:1 2 3 4 5 6 7 8 9 10 11 12 13 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分=团队得分+个人贡献分 个人贡献分: 个人 ...

  2. mysql 解压版安装

    1.官网下载压缩包 2.解压 3.配置环境变量 添加系统环境变量  MYSQL_HOME 值为解压的主目录,例如     D:\mysql-5.7.25-winx64 修改Path 环境变量,点击编辑 ...

  3. 数据标记系列——标记工具Imagtagger

    https://github.com/bit-bots/imagetagger 待有空说一说!

  4. Python——Django-模板

    一.模板的种类 1.变量 {{变量名}} 2.语句类{% %} 2.1 {%for i in booklist%} {{i}} {%endfor%} 2.2 {%if 10>5%} {%else ...

  5. 精心收集的 95 个超实用的 JavaScript 代码片段( ES6+ 编写)

    https://www.html.cn/archives/8748#table-of-contents https://www.haorooms.com/post/js_regexp

  6. iOS 利用高德地图WMS服务

    Demo:  https://github.com/xushiyou23/AMapTesting 转: 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net ...

  7. 使用with语句优化pymysql的操作

    一.with语句的好处 with语句的好处在于,它可以自动帮我们释放上下文,就比如文件句柄的操作,如果你不使用with语句操作,你要先open一个文件句柄,使用完毕后要close这个文件句柄,而使用w ...

  8. 在Ubuntu上使用离线方式快速安装K8S v1.11.1

    在Ubuntu上使用离线方式快速安装K8S v1.11.1 0.安装包文件下载 https://pan.baidu.com/s/1nmC94Uh-lIl0slLFeA1-qw v1.11.1 文件大小 ...

  9. BZOJ3784树上的路径

    题目描述 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a<b.将这n*(n-1)/2个距离从大到小排序, ...

  10. Linux-服务器创建swap交换分区

    服务器 swap 交换分区制作 作用:‘提升‘ 内存的容量,防止OOM(Out Of Memory) 查看当前的交换分区 # cat /proc/swaps # free -m # swapon -s ...