google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.18k stars 580 forks source link

Member variables (getters/setters) are too slow. #1625

Open vojtajina opened 9 years ago

vojtajina commented 9 years ago

This is a discussion.

Currently we generate getters/setters for member variables (class fields), so that we can do the runtime type check. This has two issues:

What can we do?

What do you think? @arv @vicb

arv commented 9 years ago

Are runtime asserts slower on accessors than methods, or is it accessors that are slower in general?

vojtajina commented 9 years ago

Accessors are slow, even without asserts.

arv commented 9 years ago

Since accessors and instance properties have different semantics I think we would need to do what TS does and make these instance properties in all cases. However, that makes the runtime type asserts unreliable.

vicb commented 9 years ago

@vojtajina

also see #1530

vicb commented 9 years ago

TS does not generate accessors because there is no runtime type check:

class Base {
    a: number = 0;
    consructor() {      
    }
}

class Child extends Base{
    b: string = 'foo';
    constructor() {
        super();
        this.a = this.b;
    }       
}

console.log(JSON.stringify(new Child()));

TS would detect an error on this.a = this.b. You can still execute the generated JS and get {"a":"foo","b":"foo"}.

With the current traceur JSON.stringify would return mangled property names, ie {"$__0":"foo","$__1":"foo"}.

I can not think of a way of adding type assertions on class fields and still have the correct output for JSON.stringify() - adding assertions on all the field accesses in a class would not work for inherited fields.

If we decide that the output of JSON.stringify() should keep the original names (which is a sensible choice) then we should stop generating accessors. It also means that the field types won't be used any more. Class field types can be used later when/if traceur implements compile time type checking (as TS does). They will also be used in Angular when the target language is Dart.

vojtajina commented 9 years ago

The debugging issue could be solved by naming the properties, like @vicb suggested. There is another problem: JSON.stringify will produce different output with getters.

I see three options: