js对比for、forEach、map遍历数组速度
function a() {
var arr = new Array(1000000);
for(var i = 0; i < arr.length;i ++) {
arr[i] = i;
}
var start1 = new Date().getTime();
for(var i = 0; i < arr.length;i ++) {
arr[i] = i+1;
}
var stop1 = new Date().getTime();
console.info(stop1-start1);
var start2 = new Date().getTime();
arr.forEach(function(value,index,array) {
arr[i] = i+1;
});
var stop2 = new Date().getTime();
console.info(stop2-start2);
var start3 = new Date().getTime();
arr.map(function(index,value,array) {
arr[i] = i+1;
});
var stop3 = new Date().getTime();
console.info(stop3-start3);
}
a();
>>>4
>>>29
>>>155
js对比for、forEach、map遍历数组速度的更多相关文章
- js中的for-of循环遍历数组
遍历数组要怎么做,可能你首先想到的会是for循环,当然for循环在JavaScript 刚萌生的时候就出现了,想到它也是理所当然的 var a=[[1,2],[3,4],5] for(var i=0; ...
- C# foreach 循环遍历数组
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- IT兄弟连 Java语法教程 数组 使用foreach循环遍历数组元素
从JDK5之后,Java提供了一种更简单的循环:foreach循环,也叫作增强for循环,这种循环遍历数组和集合更加简洁.使用foreach循环遍历数组和集合元素时,无需获得数组或集合的长度,无需根据 ...
- Java基础(6):foreach 方法遍历数组
foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英文字面意思理解 foreach 也就是“ for 每一个”的意思,那 ...
- JS基础语法---for循环遍历数组
for循环遍历数组 要显示数组中的每个数据,可以如下: var arr=[10,20,30,40,50]; //显示数组中的每个数据 console.log(arr[0]); console.log( ...
- js中 map 遍历数组
map 方法会迭代数组中的每一个元素,并根据回调函数来处理每一个元素,最后返回一个新数组.注意,这个方法不会改变原始数组. 在我们的例子中,回调函数只有一个参数,即数组中元素的值 (val 参数) , ...
- mybatis问题。foreach循环遍历数组报错情况,及其解决方法
根据条件查询数据列表,mybatis查询代码如下 如果只查询属于特定部门拥有的数据权限.这需要用 String[ ] codes保存当前部门及其子部门的部门编码. 所以需要在mybatis中遍历编码数 ...
- MyBatis foreach标签遍历数组
有时候开发中需要根据多个ID去查询,可以将ID封装为List或者数组然后使用MyBatis中的foreach标签构建in条件. 这里我将ID封装为String[]作为参数. <select id ...
- 使用ECMAscript5中的forEach函数遍历数组
1 var a = [1,2,3]; 2 a.forEach(function(value,index,arr){ 3 arr[index] = value + index; 4 }) 5 conso ...
随机推荐
- Android:阻止输入法将图片压缩变形
Scrollview定义中添加一行: android:isScrollContainer="false"
- poj 1635
有根树同构.参考论文<hash在....> #include <iostream> #include <fstream> #include <algorith ...
- volatile非原子性的示例
volatile非原子性的示例 package com.stono.thread2.page124; public class MyThread extends Thread { volatile p ...
- jabberNet 修改花名册条目的昵称
修改昵称,这么简单的功能,在jabberNet里怎么实现? 翻遍了jabberNet里的代码,jabber.client.RosterManager也,JabberClient也,似乎都没有现成的方法 ...
- bzoj2503 相框——思路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 思路题: 首先,这种问题应该注意到答案只跟度数有关,跟其他什么连接方法之类的完全无关: ...
- Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time
题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...
- [Apple开发者帐户帮助]五、管理标识符(1)注册应用程序ID
一个应用程序ID标识的配置设定档中的应用程序.它是一个由两部分组成的字符串,用于标识来自单个开发团队的一个或多个应用程序.有两种类型的应用程序ID:用于单个应用程序的显式应用程序ID,以及用于一组应用 ...
- ASP之ViewState和IsPostBack
没怎么写过ASPX页面,今天在做增删改的界面的时候,修改出了问题. 根据传过来的ObjectID加载页面数据,赋值给TextBox控件后,修改控件的值回写数据库,发现值没有变化. 简单的例子如下: 然 ...
- C#之仿魔兽登录
不多废话,直接上效果图: 1录窗体 对应的代码: using System; using System.Collections.Generic; using System.ComponentModel ...
- JavaScript(基于react+dva)
变量声明 const 和 let:分别表示常量和变量 模板字符串 const user = 'world'; console.log(`hello ${user}`); // hello world ...