js-mythbusters icon indicating copy to clipboard operation
js-mythbusters copied to clipboard

Add anonynous function tip

Open Kikobeats opened this issue 7 years ago • 2 comments

reasons to name functions:

  • Stack Trace
  • Dereferencing
  • Code Reuse

Source: http://elijahmanor.com/talks/js-smells/#/11/3 Related: https://www.youtube.com/watch?v=_0W_822Dijg

Kikobeats avatar Aug 31 '16 10:08 Kikobeats

From https://hackernoon.com/how-to-make-the-fastest-promise-library-f632fd69f3cb

Avoid creating unnecessary variables, functions and instances Following this principle helps avoiding unnecessary memory allocations. For example

function sum(array) {
  return array.reduce(function iterator(result, num) {
    return result + num;
  });
}


sum([1, 2, 3]); // 6

When sum is called, the iterator function is always created. It is one of unnecessary memory allocations. The code is rewritten to follow next example.

function iterator(result, num) {
  return result + num;
}

function sum(array) {
  return array.reduce(iterator);
}

sum([1, 2, 3]); // 6

The code avoids making unnecessary functions.

Kikobeats avatar Sep 17 '17 09:09 Kikobeats

Another good reason to name a function, or to save some intermediate result in a named variable, is that it gives a free opportunity to describe your intent, if you're somewhat good with descriptive naming. Very useful for future maintenance programmers, probably your future self.

mk-pmb avatar Mar 14 '20 23:03 mk-pmb