Reputation: 2046
Is there any ability in future ECMAScript standards and/or any grunt/gulp modules to make an inline functions (or inline ordinary function calls in certain places) in JavaScript like in C++?
Here an example of simple method than makes dot product of vectors
Vector.dot = function (u, v) {
return u.x * v.x + u.y * v.y + u.z * v.z;
};
Every time when I'am writing somethink like
Vector.dot(v1, v2)
I want be sure that javascript just do this calculations inline rather then make function call
Upvotes: 5
Views: 1962
Reputation:
Modern JS engines already inline functions when they identify that it is possible and useful to do that.
See http://ariya.ofilabs.com/2013/04/automatic-inlining-in-javascript-engines.html. Quote:
If you always worry about manual function inlining, this is a good time to revisit that thought. Simply write the code to be readable even if it means breaking the code into multiple small functions! In many cases, we can trust the modern JavaScript engines to automatically inline those functions.
I suppose you could write a pre-processor to handle inline
keywords and do the necessary source code rewriting (and then wire it into gulp or grunt if you insist), but that would seem to be quite complex, and frankly most likely not worth the trouble.
Upvotes: 2
Reputation: 5334
Given that the OP asks for performance, I will try to provide an answer.
If you are optimizing for the V8 engine, you can check the following article to see which functions are inlined, and how deoptimization affects your code.
http://floitsch.blogspot.com/2012/03/optimizing-for-v8-inlining.html
For example, if you want to see if Vector.dot
is inlined, use the following command line where script.js
contains both your definition and calling code:
d8 --trace-inlining script.js
The optimization algorithm differs from engine to engine, but the inlining concept should be pretty much the same. If you want to know about other engines, please modify the question to including the exact engine in order to get some insights from JS engine experts.
Upvotes: 3