Skip to content

Discussion: Babel plugin to optimize javascript engine code (like v8) #9

@hzoo

Description

@hzoo

EDIT: can also be an eslint rule/plugin or a combination of both

Pardon my inexperience/lack of knowledge in all of this but wanted to get some ideas down so we can all learn!

Also ref the thread: https://twitter.com/left_pad/status/814967401276182529
Like in: http://richardartoul.github.io/jekyll/update/2015/04/26/hidden-classes.html

Always instantiate your object properties in the same order so that hidden classes, and subsequently optimized code, can be shared.
Adding properties to an object after instantiation will force a hidden class change and slow down any methods that were optimized for the previous hidden class. Instead, assign all of an object’s properties in its constructor.
Code that executes the same method repeatedly will run faster than code that executes many different methods only once (due to inline caching).

We should be able to lint/add a runtime check for these?

1  function Point(x,y) {
2    this.x = x;
3    this.y = y;
4  }
5 
7  var obj1 = new Point(1,2);
8
9 obj1.a = 5;

So for the above example we could make a few warnings/errors.

We could warn that a property a is being created on line 9 but the constructor starting on line 1 doesn't contain this.a? So it could be added there, or ask that it be not used if possible?

because

10 obj1.a = 5;
11 obj1.b = 10;
12
13 obj2.b = 10;
14 obj2.a = 5;

have different transition paths we can warn on this kind of thing (moving it to the constructor should fix it as well?)

In some cases we can do a static check for these kinds of things but otherwise we will probably want to just do some code instrumentation around objects/etc to do runtime check/analysis instead?

Maybe we can use something like ES2015 Proxy (or just wrap certain variables in functions), or is it necessary to instrument v8 as well or use something like IRHydra?

Might be cool to see some data around the kinds of objects created (how many times) and what properties are available and added/removed at what lines in the codebase.

Other deopts; https://github.com/petkaantonov/bluebird/wiki/Optimization-killers

cc @addyosmani, @lelandrichardson, @jordwalke

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions