4 ms·
The problem with #1 is that the minifier (e.g. http://closure-compiler.appspot.com/home http://closure-compiler.appspot.com/home) can't optimize as much. -----
by wildpeaks 12y ago
The problem with #1 is that the minifier (e.g. http://closure-compiler.appspot.com/home http://closure-compiler.appspot.com/home) can't optimize as much.
---------
Example using strings:
var obj = {
start: function(){
'use strict';
console.log('start');
},
stop: function(){
'use strict';
console.log('stop');
}
};
exports.onDummy = function(success){
'use strict';
var method = (success ? 'start' : 'stop');
obj[method]();
};
results in:
var a={start:function(){console.log("start")},stop:function(){console.log("stop")}};exports.a=function(b){a[b?"start":"stop"]()};
---------
Same thing, using if..else:
var obj = {
start: function(){
'use strict';
console.log('start');
},
stop: function(){
'use strict';
console.log('stop');
}
};
exports.onDummy = function(success){
'use strict';
if (success){
obj.start();
} else {
obj.stop();
}
};
results in the much shorter:
exports.a=function(a){a?console.log("start"):console.log("stop")};