Javascript for loop efficiency

后端 未结 5 680
我寻月下人不归
我寻月下人不归 2021-01-04 10:01

Is

for (var i=0, cols=columns.length; i

more efficient than

for (var i=0; i

        
5条回答
  •  难免孤独
    2021-01-04 10:38

    Any expression that's in the second portion of a for will be evaluated once per loop.

    So, here, with your second proposition, yes, columns.length will be calculated each time the condition is checked -- which would make the first proposition faster than the second one.


    (That's true for many other languages, btw)

提交回复
热议问题