Refactoring for slight performance improvement

Replaced some ECMAScript5 Array method with anonymous function as described in TODO. A few method calls still stay to keep scope clean at the top level. Also refactored places to call Array.length for performance improvement.