JS数组遍历方法的性能对比
Posted by Mars . Modified at
JS数组遍历方法的性能对比
试验了几种方法:
- for正向遍历;
- for反向遍历;
- for…of 遍历;
- while循环;
- forEach方法;
- map方法;
- for…in遍历;
结果如下:(10000000元素数)
可见,性能从优到劣:
- for循环遍历最快,逆向比正向更快;
- while循环次之;
- forEach、map次之;
- of循环;
- for…in遍历性能极差,不要使用。
</b>
Posted by Mars . Modified at
JS数组遍历方法的性能对比
试验了几种方法:
结果如下:(10000000元素数)
可见,性能从优到劣:
</b>