javascript - Are these valid Loop optimizations? -


if following, considered un-optimal (performance wise)?

// optimal? foreach (var fruit in basket.fruits()) {   // .... }  // this? (var = 0; < basket.fruits().length; ++i) {   // .... }  // alternate way. var fruits = basket.fruits(); foreach (var fruit in fruits)  {   // .... } 

im looking answers applicable .net javascript.

thank you.

both equally performant. basket.fruits() returns object , javascript create iterator object.

another solution use

object.keys(basket.fruits()).foreach(function(fruit) {   // ... }); 

either way, don't focus on kind of optimization. javascript fast , there many automatic optimizations. should focusing on making code legible possible. after it's done, if find slow part, focus on optimizing part.


according edit, store array's length in variable in initialization expression of loop. predicate expression can compare i<len instead of computing array length each iteration.

for (var i=0; len=basket.fruits().length; i<len; i++) ... 

Comments

Popular posts from this blog

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -