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

 For  (var a = 0; a & lt; zodiac; a ++) {for (arr in var b) {arr [b] ++; }}  

JS 5.1 array.forEach (2.1ms):

 for  ( Var C = 0; c & lt; zodiac; c ++) {arr.forEach (function (e, i, a) {arr [i] ++;}); }  

i = 0 for the fastest (default) I & lt; Array.lambi; I ++ (1.1ms):

 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 , but it seems that the default Fastest!

My main question is:

  • What is the reason behind poor performance of in the array
  • < Li> What is the difference between the default Iterator and the new (what is the requirement of other Iterator method)?

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

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -