js-mythbusters
js-mythbusters copied to clipboard
Add anonynous function tip
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
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.
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.