Closed as not planned
Description
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
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
bla() {
return this.greet() + this.greeting;
}
moreBla() {
return this.greet() + this.greeting;
}
blablabla() {
return this.greet() + this.greeting;
}
}
class Hello extends Greeter {
greet() {
return "Goodbye";
}
}
let greeter = new Greeter("world");
let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
var x = [greeter.bla(), greeter.moreBla(), greeter.blablabla()];
console.log(x);
}
document.body.appendChild(button);
Actual behavior:
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
Greeter.prototype.bla = function () {
return this.greet() + this.greeting;
};
Greeter.prototype.moreBla = function () {
return this.greet() + this.greeting;
};
Greeter.prototype.blablabla = function () {
return this.greet() + this.greeting;
};
return Greeter;
}());
var Hello = (function (_super) {
__extends(Hello, _super);
function Hello() {
_super.apply(this, arguments);
}
Hello.prototype.greet = function () {
return "Goodbye";
};
return Hello;
}(Greeter));
var greeter = new Greeter("world");
var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
alert(greeter.greet());
var x = [greeter.bla(), greeter.moreBla(), greeter.blablabla()];
console.log(x);
};
document.body.appendChild(button);
Suggested behavior:
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
var p = Greeter.prototype;
p.greet = function () {
return "Hello, " + this.greeting;
};
p.bla = function () {
return this.greet() + this.greeting;
};
p.moreBla = function () {
return this.greet() + this.greeting;
};
p.blablabla = function () {
return this.greet() + this.greeting;
};
return Greeter;
}());
var Hello = (function (_super) {
__extends(Hello, _super);
function Hello() {
_super.apply(this, arguments);
}
var p = Hello.prototype;
p.greet = function () {
return "Goodbye";
};
return Hello;
}(Greeter));
var greeter = new Greeter("world");
var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
alert(greeter.greet());
var x = [greeter.bla(), greeter.moreBla(), greeter.blablabla()];
console.log(x);
};
document.body.appendChild(button);