javascript - Is it more performant to write named functions in node js? -


I am developing an app in node js. I was using anonymous functions for callback purpose then I referenced the site where I found writing the named function is the best way to coding.

But when I started writing code in designated function format, my lines of code increased, my number of functions increased, is it better to write a named function for each callback?

The code that is intelligent

code, function declaration is fastest, this applies especially to instances where you are applying the same function multiple times. East. Using Collection Lookup, Array Recurrence, etc ...

, you can quickly see the difference.

  ☃ seth: ~ / node / function-performance $ node test js benami x 121 ops / second ± 8.31% (64 run samples) function expression x 137 ops / second 5.25% (72 runs) Samples) Function Declaration X 165 Ops / sec ± 0.84% ​​(86 runs samples) Fastest Declaration  

This is using the following code to test:

  "strict use"; Var requires Benchmark = ('Benchmark') var suite = New Benchmark. Oat works = [1,2,3,4,5,6,9,8,9,10,11,12,13,14,14,15]; Var myEventHandler = function () {// do something}; Function myEventHandler2 () {// do something}; Suite.add ('Anonymous', function) (for (var i = 0; i & lt; 10000; ++ i) {stuff.forEach (function () {// do something}}}) .add ( 'Function expression', function (for (var i = 0; i & lt; 10000; ++ i) {stuff.forEach (myEventHandler);}}) .add ('function declaration', function () { For (Var i = 0; i & lt; 10000; ++ i) {stuff.forEach (myEventHandler2);}}) .on ('cycle', function (e) {console.log (string (e.target) }}). ("Full", function () {console.log ('fastest' + this.filter ('fastest'). ('Name'))). Run ({'async' : True});  

goes for that too.

To support Zeekar's comment - this d If you are not running the ceremony more than once in a given example, you usually go with anonymous tasks, though it can clear your invokesments while using function announcements.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -