JS数组遍历方法的性能对比

Posted by Mars . Modified at

JS数组遍历方法的性能对比

试验了几种方法:

  1. for正向遍历;
  2. for反向遍历;
  3. for…of 遍历;
  4. while循环;
  5. forEach方法;
  6. map方法;
  7. for…in遍历;

结果如下:(10000000元素数)

JS数组遍历方法的性能对比

可见,性能从优到劣:

  • for循环遍历最快,逆向比正向更快;
  • while循环次之;
  • forEach、map次之;
  • of循环;
  • for…in遍历性能极差,不要使用。

</b>

Keywords: JavaScript
previousPost nextPost
已经有 1000000 个小伙伴看完了这篇推文。