Optimized Loops


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //Mostly when people are writing loops, it looks like this:
  2.  
  3. for(var i:int = 0; i < list.length; i++)
  4. {
  5. var item:MyItem = list[i];
  6. item.doSomething();
  7. item.doAnotherThing();
  8. }
  9.  
  10. //Take a look at the improved loop:
  11.  
  12. var item:MyItem;
  13. var total:uint = list.length;
  14. for(var i:uint; i < total; i++)
  15. {
  16. item = list[i] as MyItem;
  17. item.doSomething();
  18. item.doAnotherThing();
  19. }
  20.  
  21. //Want a more freaky way to define the same optimized loop?
  22.  
  23. for(var i:uint, item:MyItem, total:uint = list.length; i < total; i++)
  24. {
  25. item = list[i] as MyItem;
  26. item.doSomething();
  27. item.doAnotherThing();
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.