

Ask HN: Private functions more likely to be JIT compiled in JavaScript? - malandrew

I've been searching for an answer to this for the last few hours and I was wondering if someone working on a tracing JavaScript JIT compiler could answer this question.<p>Are named private JavaScript functions accessed from a constructor class more likely to be tracing JIT-compiled to [native code] than methods on that class that are copied onto every instance of that class? I would imagine that they would be because they all of a sudden become a code hot spot for a frequently instantiated class.<p>i.e. consider the following constructor class.<p><pre><code>    (function(module) {

        function Foo() {
            // instantiate foo
            this.bar();
            this.baz();
        };

        Foo.prototype.bar = function() {
            // do bar
        };

        Foo.prototype.baz = baz;

        function baz() {
            // do baz
        }
    
        module.exports = Foo;

    })(NameSpace);
</code></pre>
In the above class, is the hoisted function baz much more likely to be compiled to [native code] by modern JIT compilers when Foo() class is instantiated many times? If so, what's the threshold for a function to become a hotspot and what kind of performance gain can be expected from doing this on any frequently instantiated class?<p>Obviously, if this is a performance improvement, it would still come with the caveat that you can no longer use the keyword this inside baz() nor can I bind it to this with Object.bind(), however this could be solved by passing in this as argument self, right? i.e.<p><pre><code>    (function(module) {

        function Foo() {
            // instantiate foo
            this.bar();
            this.baz(this);
            console.log(this.qux);  // prints 'quux'
        };

        Foo.prototype.bar = function() {
            // do bar
        };

        Foo.prototype.baz = baz;

        function baz(self) {
            self.qux = 'quux';
            // do baz
        }
    
        module.exports = Foo;

    })(NameSpace);

</code></pre>
Also, if anyone can point me to more details on the exact conditions under which my own JavaScript code is JIT compiled to [native code] that would be most awesome.
======
brittonrt
I would also like to know more about this...

------
stefs
are there even any tracing JITs for javascript left?

