I recently developed a module in TS, meant for the web, so it is also being minified.
I reviewed the output file and found that the word prototype is not minified (naturally, since it is a reserved word). But my module ended up having 153 occurrences of prototype, which amounted to 1.34 KB out of a total of 21KB in the minified file (I am using Google Closure Compiler with the ADVANCED_OPTIMIZATIONS flag).
So I had an idea that can reduce that by putting the prototype into a variable before using it in every class definition.
Example:
Code
classGreeter{greeting: string;constructor(message: string){this.greeting=message;}greet(){return"Hello, "+this.greeting;}bla(){returnthis.greet()+this.greeting;}moreBla(){returnthis.greet()+this.greeting;}blablabla(){returnthis.greet()+this.greeting;}}classHelloextendsGreeter{greet(){return"Goodbye";}}letgreeter=newGreeter("world");letbutton=document.createElement('button');button.textContent="Say Hello";button.onclick=function(){alert(greeter.greet());varx=[greeter.bla(),greeter.moreBla(),greeter.blablabla()];console.log(x);}document.body.appendChild(button);Actual behavior:
var__extends=(this&&this.__extends)||function(d,b){for(varpinb)if(b.hasOwnProperty(p))d[p]=b[p];function__(){this.constructor=d;}d.prototype=b===null ? Object.create(b) : (__.prototype=b.prototype,new__());};varGreeter=(function(){functionGreeter(message){this.greeting=message;}Greeter.prototype.greet=function(){return"Hello, "+this.greeting;};Greeter.prototype.bla=function(){returnthis.greet()+this.greeting;};Greeter.prototype.moreBla=function(){returnthis.greet()+this.greeting;};Greeter.prototype.blablabla=function(){returnthis.greet()+this.greeting;};returnGreeter;}());varHello=(function(_super){__extends(Hello,_super);functionHello(){_super.apply(this,arguments);}Hello.prototype.greet=function(){return"Goodbye";};returnHello;}(Greeter));vargreeter=newGreeter("world");varbutton=document.createElement('button');button.textContent="Say Hello";button.onclick=function(){alert(greeter.greet());varx=[greeter.bla(),greeter.moreBla(),greeter.blablabla()];console.log(x);};document.body.appendChild(button);Suggested behavior:
var__extends=(this&&this.__extends)||function(d,b){for(varpinb)if(b.hasOwnProperty(p))d[p]=b[p];function__(){this.constructor=d;}d.prototype=b===null ? Object.create(b) : (__.prototype=b.prototype,new__());};varGreeter=(function(){functionGreeter(message){this.greeting=message;}varp=Greeter.prototype;p.greet=function(){return"Hello, "+this.greeting;};p.bla=function(){returnthis.greet()+this.greeting;};p.moreBla=function(){returnthis.greet()+this.greeting;};p.blablabla=function(){returnthis.greet()+this.greeting;};returnGreeter;}());varHello=(function(_super){__extends(Hello,_super);functionHello(){_super.apply(this,arguments);}varp=Hello.prototype;p.greet=function(){return"Goodbye";};returnHello;}(Greeter));vargreeter=newGreeter("world");varbutton=document.createElement('button');button.textContent="Say Hello";button.onclick=function(){alert(greeter.greet());varx=[greeter.bla(),greeter.moreBla(),greeter.blablabla()];console.log(x);};document.body.appendChild(button);
I recently developed a module in TS, meant for the web, so it is also being minified.
I reviewed the output file and found that the word
prototypeis not minified (naturally, since it is a reserved word). But my module ended up having 153 occurrences ofprototype, which amounted to 1.34 KB out of a total of 21KB in the minified file (I am using Google Closure Compiler with the ADVANCED_OPTIMIZATIONS flag).So I had an idea that can reduce that by putting the
prototypeinto a variable before using it in every class definition.Example:
Code
Actual behavior:
Suggested behavior: