Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript inline functions like in C++

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

like image 602
Vlad Ankudinov Avatar asked Sep 07 '15 10:09

Vlad Ankudinov


People also ask

Is there inline function in C?

Inline Function are those function whose definitions are small and be substituted at the place where its function call is happened. Function substitution is totally compiler choice.

What is __ inline in C?

The __inline keyword suggests to the compiler that it compiles a C or C++ function inline, if it is sensible to do so. The semantics of __inline are exactly the same as those of the inline keyword.

What is inline function in JavaScript?

An inline function is a javascript function, which is assigned to a variable created at runtime. You can difference Inline Functions easily with Anonymous since an inline function is assigned to a variable and can be easily reused.

What is meant by inline function in C++?

Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.


1 Answers

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.

like image 59
FelisCatus Avatar answered Nov 02 '22 08:11

FelisCatus