3

What is the best way to structure your js for performance speed as well as page load speed, less strain on the browser.

var myFunc = function(){/*do stuff*/}

or

function myFunc(){/*do stuff*/}
Charlie
  • 22,886
  • 11
  • 59
  • 90
Mike
  • 623
  • 6
  • 26

1 Answers1

3

There is no difference in performance between these two methods.

var myFunc = function(){/*do stuff*/}

or

function myFunc(){/*do stuff*/}

However the first function is created only when the line is executed. But the second function is available even when the script starts to execute.

It is important to understand Javascript Hoisting to explain this behavior.

Here is another useful article on this.

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • to what I am seeing, Honestly it would be best to claim each function as a var. But honestly this really does not sound correct. When I step through the code I see the vars initialize. Even know the function is not being executed. Seems like this would take up more memory. Second link was helpful. Thanks. – Mike May 11 '16 at 15:21
  • 1
    //Seems like this would take up more memory - Please support this by the details of your observation. – Charlie May 11 '16 at 15:25