I am trying to understand optimization to improve minification in JavaScript
While in uniform.js source code I found it (on line # 333 ):
/**
* Meaningless utility function. Used mostly for improving minification.
*
* @return false
*/
function returnFalse() {
return false;
}
This function is called in some places (trend seems to be in situations where a value is false
used in a JavaScript JavaScript object like this (from line # 351 )
bindMany($elem, options, {
'selectstart dragstart mousedown': returnFalse
});
But not in situations where false is explicitly returned. My question is why is it said to improve minification? My first thought was that by pulling false
into a function, the function could be renamed to say "a" so the places where false
expected could be represented as a()
, but that doesn't seem to give much benefit from what I think , this is the cost of the additional function when executed. Something much more obvious that I don't understand about this?
source to share
You should notice that in your example returnFalse
it is not called, it passes. It is used to prevent default behavior for provided events.
This method of using a utility function to prevent default behavior prevents the need to create new anonymous functions to do the same thing multiple times:
$('foo').on('something', function () {
return false;
});
//minified to something like:
$(a).on(b,function(){return !1});
against
$('foo').on('something', returnFalse);
//minified to something like:
function c(){return !1}
$(a).on(b,c);
That said, this is a fallacious minification tool, as jQuery allows false
event handlers to be passed instead of to prevent default behavior:
$('foo').on('something', false);
//minified to something like:
$(a).on(b,!1);
source to share