Ask HN: Private functions more likely to be JIT compiled in JavaScript?
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.
[+] [-] brittonrt|13 years ago|reply
[+] [-] stefs|13 years ago|reply