javascript - Cause of performance difference in various iterators -
Javascript while playing with the new array functions, I saw some major differences in the display : jsfiddle:
Sir I (67.2ms) for slowest JS 5.1 i = 0 for the fastest (default) I & lt; Array.lambi; I ++
(1.1ms): For
(var a = 0; a & lt; zodiac; a ++) {for (arr in var b) {arr [b] ++; }}
array.forEach
(2.1ms): for
( Var C = 0; c & lt; zodiac; c ++) {arr.forEach (function (e, i, a) {arr [i] ++;}); }
for (var d = 0; d & lt; zodiac; d ++) {for (var e = 0; e & lt; arr.length; e ++) {arr [E] ++; }}
I thought it was a good practice to use sir i instead of default My main question is: , but it seems that the default Fastest!
all properties on a in-in
loop object Repeats, not just array values. In this way, Orem has to retrieve those property values, creating only one array index such that other two methods are doing. In addition to having the wrong way to rearrange the array, I am not surprised that it is slow but regardless of speed, it is the wrong way to iterate the array. If anyone adds a recurrence method or property to the array
prototype, that property will be displayed in your in-in
array repetition.
.for (former)
is convenience and sometimes feature made by callback is convenient (especially when you want to use the array index in an async callback) . It can be slow because it has a function call with each frequency vs logic. There is no additional function call on every move in your third method.
FYI, .forEach ()
The method is the slowest of all in Firefox, so it is also different by the browser.
Comments
Post a Comment