Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js的map和forEach到底哪个速度快? #17

Closed
xiaohesong opened this issue Dec 26, 2018 · 0 comments
Closed

js的map和forEach到底哪个速度快? #17

xiaohesong opened this issue Dec 26, 2018 · 0 comments
Labels
help wanted tips 一些不错的想法

Comments

@xiaohesong
Copy link
Owner

今天看到一篇文章,一个大神写的。可以看Speed Considerations这个地方,他说他那里测试是mapforEach快不少。

image

我觉得不太可能,毕竟map是返回一个新的数组呀。

//example.js
mapTag = "Map Spent Time is"
eachTag = "Each Spend Time is"
mapArr = eachArr = [...Array(1000000)]

console.time(mapTag)
mapArr.map((i, index) => {
  return index * 2
})
console.timeEnd(mapTag)

console.time(eachTag)
eachArr.forEach((i, index) => {
  return eachArr[index] = index * 2
})
console.timeEnd(eachTag)

从上面这个例子可以发现不是这样的。

不对呀,jsperf应该不会出问题的。难道是forEach有提升吗?找了下,没有找到相关的资料,值看到了说17年八月初forEach有提升十倍,那也不对呀,上面的文章提到的是2017年12月份。

jsperf的工作方式

比较好奇啊,这是为啥呐。

@xiaohesong xiaohesong added help wanted tips 一些不错的想法 labels Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted tips 一些不错的想法
Projects
None yet
Development

No branches or pull requests

1 participant