5

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

15
  • what do you mean? like this: (function() {alert('Hello');})() ? Commented Sep 7, 2015 at 11:01
  • I can think about 5 reasons why it wouldn't affect JS code as it affects C++ code.. Commented Sep 7, 2015 at 11:02
  • 1
    The engine will most likely make intelligent decisions about what to inline. I supposed one could write a pre-processor to do this, but I doubt if it would have any meaningful performance impact. Commented Sep 7, 2015 at 11:03
  • 1
    @Hacketo He's referring to the inline keyword which caused the compiler to emit the relevant code in-place instead of generating a function call. Commented Sep 7, 2015 at 11:04
  • 1
    If you are asking about JS grammar or basics, there are only three types of functions: named, unnamed or arrow (new in ES6). If you are concerned about optimization, there is no need to mark a function as inline in JS, as the JS engines can do the optimization if they want. V8, for example, does inline some functions for performance. Commented Sep 7, 2015 at 11:05

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.