To declare or not local variables in JavaScript -


which better way of writing foo()?
foo1() or foo2()?
better in terms of:

  1. performance
  2. memory consumption
  3. variable/object entropy

assuming other factors taken care of.
ex: variable not used @ multiple places within function, etc.

        function f1() {             return 10;         }          function f2() {             return 20;         }          function foo1() {             return f1() === f2();         }          function foo2() {             var = f1();             var j = f2();             return === j;         } 

i'm not expert on this, i'll try best..

for performance, mentioned on comment use jsperf..

for memory consumption, believe foo1() better, because don't need create new variable..

for variable entropy, i'm not sure how answer point, since believe don't need variables.

hope helps.


Comments