Javascript中八种遍历方法的执行速度深度对比(4)


数组长度为10^7 chrome 52.0.2743.116 (64-bit) Firefox Developer Edition 49.0a2 (2016-08-01) Safari 9.1.1 (11601.6.17)
for   (164+161)/2 = 162.5   (26+30)/2 = 28   (30+31)/2 = 30.5  
do while   (163+157)/2 = 160   (27+25)/2 = 26   (28+27)/2 = 27.5  
for in   (78+86)/2 = 82   (0+0)/2 = 0   (0+0)/2 = 0  
forEach   (211+205)/2 = 208   (31+30)/2 = 30.5   (291+289)/2 = 290  
map   (349+282)/2 = 315.5   (24+22)/2 = 23   (259+260)/2 = 259.5  
every   (221+219)/2 = 220   (24+24)/2 = 24   (251+257)/2 = 254  
$.each   (210+215)/2 = 212.5   (2868+2789)/2 = 2828.5   (699+724)/2 = 711.5  
$(e).each   (730+669)/2 = 699.5   (4674+4722)/2 = 4698   (523+546)/2 = 534.5  

数组长度为10^8


数组长度为10^8 chrome 52.0.2743.116 (64-bit) Firefox Developer Edition 49.0a2 (2016-08-01) Safari 9.1.1 (11601.6.17)
for   (1486+1583)/2 = 1534.5   (222+238)/2 = 230   (261+251)/2 = 256  
do while   (1548+1608)/2 = 1578   (236+247)/2 = 241.5   (272+265)/2 = 268.5  
for in   (0+0)/2 = 0   (0+0)/2 = 0   (0+0)/2 = 0  
forEach   (25838+22307)/2 = 24072.5   (212+209)/2 = 210.5   (2565+2568)/2 = 2566.5  
map   (23795+22787)/2 = 23291   (215+206)/2 = 210.5   (2556+2573)/2 = 2564.5  
every   (22393+22378)/2 = 22385.5   (212+215)/2 = 213.5   (2550+2548)/2 = 2549  
$.each   (14523+14776)/2 = 14649.5   (28007+27698)/2 = 27852.5   (7109+7156)/2 = 7132.5  
$(e).each   chrome 奔溃了...   (49352+49530)/2 = 49441   (5505+4616)/2 = 5060.5  

综上, 我们发现for in 循环的性能不稳定, 猜测它可能没有进入循环. 因此将数组各元素进行如下赋值. 重新进行如下两轮测试.

var array = [], length = array.length = 1000000; for(var i=0;i<length;i++){ array[i] = 'louis'; }

数组赋值后, 数组长度为10^6


数组长度为10^6 chrome 52.0.2743.116 (64-bit) Firefox Developer Edition 49.0a2 (2016-08-01) Safari 9.1.1 (11601.6.17)
for   (21+22)/2 = 21.5   (8+10)/2 = 9   (6+5)/2 = 5.5  
do while   (22+19)/2 = 20.5   (6+6)/2 = 6   (6+5)/2 = 5.5  
for in   (178+184)/2 = 181   (318+268)/2 = 293   (413+464)/2 = 438.5  
forEach   (42+45)/2 = 43.5   (4+4)/2 = 4   (21+24)/2 = 22.5  
map   (137+153)/2 = 145   (9+8)/2 = 8.5   (38+43)/2 = 40.5  
every   (0+0)/2 = 0   (0+0)/2 = 0   (0+0)/2 = 0  
$.each   (85+84)/2 = 84.5   (15+19)/2 = 17   (37+25)/2 = 31  
$(e).each   (81+83)/2 = 82   (34+31)/2 = 32.5   (37+46)/2 = 41.5  

数组赋值后, 数组长度为10^7


数组长度为10^7 chrome 52.0.2743.116 (64-bit) Firefox Developer Edition 49.0a2 (2016-08-01) Safari 9.1.1 (11601.6.17)
for   (171+157)/2 = 164   (27+26)/2 = 26.5   (26+28)/2 = 27  
do while   (168+158)/2 = 163   (27+27)/2 = 27   (28+29)/2 = 28.5  
for in   (1469+1715)/2 = 1592   (2922+3123)/2 = 3022.5   (5755+5742)/2 = 5748.5  
forEach   (347+329)/2 = 338   (32+36)/2 = 34   (171+174)/2 = 172.5  
map   (1320+1335)/2 = 1327.5   (147+137)/2 = 142   (448+469)/2 = 458.5  
every   (0+0)/2 = 0   (0+0)/2 = 0   (0+0)/2 = 0  
$.each   (438+441)/2 = 439.5   (142+141)/2 = 141.5   (254+248)/2 = 251  
$(e).each   (876+935)/2 = 905.5   (315+328)/2 = 321.5   (450+402)/2 = 426  

可见, 对数组进行赋值后, 代码运行基本稳定.(every还不清楚为什么执行时间为0.欢迎大神告知原因.)

分析总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wypdpf.html