Skip to content

Compile Optimization: Prototype Objects #13633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
only-cliches opened this issue Jan 23, 2017 · 2 comments
Closed

Compile Optimization: Prototype Objects #13633

only-cliches opened this issue Jan 23, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@only-cliches
Copy link

only-cliches commented Jan 23, 2017

TypeScript Version: 2.1.4

While working on a recent project I was looking at the compiled javascript code and noticed "prototype" was written dozens of times, once for each public method. The library I was working on is 13Kb in size minified, and removing all the "prototypes" reduced the size to 12.5Kb, that's half a kilobyte!

Compiled JavaScript files could considerably smaller if we applied all methods to the prototype as an object, instead of one at time.

Let's take a simple class

export class foo {
    public bar() {
        alert("bar");
    }
    public baz() {
        alert("baz");
    }
}

Existing Behavior

Once we compile with tsc, we get this:

"use strict";
var foo = (function () {
    function foo() {
    }
    foo.prototype.bar = function () {
        alert("bar");
    };
    foo.prototype.baz = function () {
        alert("baz");
    };
    return foo;
}());
exports.foo = foo;

Notice that we get the "prototype" syntax twice, once for each method.

New Suggested Behavior

Instead, the compiler could combine the multiple methods into a JS object applied to the prototype, saving a good deal of space:

"use strict";
var foo = (function () {
    function foo() {
    }
    foo.prototype = {
        bar: function () {
            alert("bar");
        },
        baz: function () {
            alert("baz");
        }
    };
    return foo;
}());
exports.foo = foo;

The performance of the code should be identical and larger libs see a huge size improvement for free. For example, the BabylonJS core lib could be almost 20Kb smaller.

@RyanCavanaugh
Copy link
Member

Duplicate of #9638?

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 23, 2017
@only-cliches
Copy link
Author

Good find sir! Yes it is, closing this issue...

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants