millermedeiros / amd-utils

modular JavaScript utilities written in the AMD format
http://millermedeiros.github.com/amd-utils
142 stars 11 forks source link

code size optimization in kindOf.js #43

Closed ThFischer closed 12 years ago

millermedeiros commented 12 years ago

You do realize that both end up compiling into the "same" code right? The only thing that reduces the file size is the {}.toString instead of using Object.prototype.toString (14 bytes) but with the cost of creating a new Object instance just to get the reference to the method... and the other stuff (ternary, returns) makes code harder to read:

Original

define(function(){var b=/^\[object (.*)\]$/,c=Object.prototype.toString;return function(a){return null===a?"Null":void 0===a?"Undefined":b.exec(c.call(a))[1]}});

Yours

define(function(){var b=/^\[object (.*)\]$/,c={}.toString;return function(a){return null===a?"Null":void 0===a?"undefined":b.exec(c.call(a))[1]}});

Minified using closure compiler.

I keep the named function with the return at the bottom because sometimes people might need the same method but on a non-AMD context, so copy and paste and an automated process works well.

Also notice that "" + UNDEF will give "undefined" while the older version would return "Undefined" (capital "U").

Ignoring it since it's premature optimization.