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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -