Reputation:
Assume I have some function f(-)
defined elsewhere. I use it twice in the following type of code:
if (f(-) satisfies some condition) {
do some stuff
if (f(-) satisfies some other condition) {
do some other stuff
}
}
When the code is run, is f(-)
calculated twice (or is the interpreter "intelligent" enough to see that it is enough to calculate it once)? If so, is it recommended to define a constant x = f(-)
before that code and use that instead of f(-)
? Usually I do this, but I am not fully sure if it is necessary, in particular, when f(-)
can be computed really fast.
In case it matters, the language is JavaScript.
Upvotes: 4
Views: 1110
Reputation: 50291
if (f(-) satisfies some other condition)
will call the same function and it may never enter the block followed by this if
, since the function
satisfies the outer if
that is why it entered into that block.
Create a const and depending on that value execute next step
const isFunResult = f(-);
// expecting this function gives a boolean
if(isFunResult){
// code goes here
}
else{
// code goes here
}
You can also use switch
statement if function return multiple result .
Upvotes: 1
Reputation: 13522
To answer your question: Yes, it calculates function f(-)
TWICE.
More than it is the intelligence of the interpreter, it is the intent of the programmer. The same function can be called twice if the programmer thinks that the function might return a changed value the second time. i.e: if the function uses a new Date()
inside the function...
But, if the programmer knows that it is the same output both times (as in your case), it is better to define a constant x = f(-)
and reuse it. This will improve performance (if the function is heavy), and improves code maintainability.
Even if it is a compiler, the compiler might not be intelligent enough to detect this, unless the function is a very simple code. But as @perellorodrigo mentioned, you can use memoization
, but it is beyond the scope of this answer.
To wrap up, if you call the same function twice, it will be evaluated twice.
Upvotes: 0
Reputation: 536
In short words, the interpreter is not "intelligent" enough to see that it is enough to calculate it once. So it will call the function twice.
What you are looking for is something called memoization which is used to optimize performance for computational heavy functions to be remembered when the same parameters are passed.
If you need memoization, you could either implement that yourself in your own functions or you could use a javascript framework that supports that. Like React has the useMemo hook.
See https://reactjs.org/docs/hooks-reference.html#usememo
Usually you wont need that and just storing the result of the function in a variable will be enough for most use cases.
Upvotes: 0
Reputation: 5547
Run this to see the examples. Even if the conditions are the same.
function test_me() {
console.log("I got called");
return 99;
}
if (test_me() == 99) console.log("n=99"); // 1st function call
if (test_me() == 99) console.log("n=99"); // 2nd function call
// is different from the following
var n = test_me(); // function called once
if (n == 99) console.log("n=99"); // 1st test
if (n == 99) console.log("n=99"); // 1st test
Upvotes: 0