Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between function declarations vs function expressions

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*/}
like image 930
Mike Avatar asked Jul 21 '26 02:07

Mike


1 Answers

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.

like image 159
Charlie Avatar answered Jul 22 '26 14:07

Charlie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!