Javascript / jQuery re design question: performance

Is there a performance / memory difference between the three following styles?

Figure A:

var func = function() { // do some magic } $("#div").somePlugin({someEvent: func}); 

Figure B:

 $("#div").somePlugin({someEvent: function() { // do some magic }); 

Illustration C:

 function func() { // do some magic } $("#div").somePlugin({someEvent: func}); 
+4
source share
2 answers

There may be a small (slightly weak) performance for expressing a function:

 var func = function(){ }; 

This is called the so-called function expression . function statement , on the other hand, is your third example:

 function func(){ } 

Function statements are internally converted to function expressions using ECMA-/Javascript , so the reason it might (!) Be a little faster, but really, nothing to worry about.

Example B: shows an anonymous function , which also does not affect the performance of A and C.

+3
source

There is no performance that could be spoken about, it is rather a reuse / style, I would say. In both A and C, func becomes reusable, which can be useful in certain situations. With B, you encapsulate functionality that is often desired.

I prefer C, as it is cleaner to read and allow reuse without refactoring.

0
source

All Articles