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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Vlad Ankudinov
  • 1,936
  • 1
  • 14
  • 22
  • what do you mean? like this: `(function() {alert('Hello');})()` ? – Amir Hossein Baghernezad Sep 07 '15 at 11:01
  • I can think about 5 reasons why it wouldn't affect JS code as it affects C++ code.. – David Haim Sep 07 '15 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. –  Sep 07 '15 at 11:03
  • please add sample examples or define "inline functions" because I think you should simply learn javascript basics – Hacketo Sep 07 '15 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. –  Sep 07 '15 at 11:04
  • Have a look at this: http://stackoverflow.com/questions/2539205/javascript-inline-function-vs-predefined-functions – Vedant Terkar Sep 07 '15 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. – FelisCatus Sep 07 '15 at 11:05
  • no, I mean replacement function call with function body to improve performance – Vlad Ankudinov Sep 07 '15 at 11:05
  • Given `function a() {return 5}`Inlining `a = x()` would become `a = 5` – Ruan Mendes Sep 07 '15 at 11:07
  • @VedantTerkar That question talks about a different meaning of inlining. –  Sep 07 '15 at 11:09
  • I dont think it would **improve** performance. they are all the same ... – Amir Hossein Baghernezad Sep 07 '15 at 11:09
  • three ways `a = function() { // do whatever }` 2: `function a () { // do whatever }` 3: `(function() { // do whatever })()` which the third is an unnamed function expression and call (both). – Amir Hossein Baghernezad Sep 07 '15 at 11:12
  • I've updated my answer to provide instructions to analyze the inlining regarding your `Vector.dot` function call in V8. – FelisCatus Sep 07 '15 at 11:22
  • related post : http://stackoverflow.com/questions/6009411/tool-to-automatically-inline-javascript-function-calls – Hacketo Sep 07 '15 at 11:23
  • Mind that if you have on-place operations ( meaning scale(u,2) changes u instead of returning a new vector ), then inlined Vector.dot(scale(u,2), v); will scale u by 8 and return the wrong result. – GameAlchemist Sep 07 '15 at 11:24

2 Answers2

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.

FelisCatus
  • 5,054
  • 2
  • 21
  • 25
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.