top | item 5516628

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

3 points| malandrew | 13 years ago | reply

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.

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.

i.e. consider the following constructor class.

    (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);
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?

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.

    (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);

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.

2 comments

order
[+] brittonrt|13 years ago|reply
I would also like to know more about this...
[+] stefs|13 years ago|reply
are there even any tracing JITs for javascript left?