George Hess
George Hess

Reputation: 689

Variables vs Expressions as Javascript Arguments

Are there any performance issues with passing an argument as an expression, instead of first making it a variable?

someFunction( x+2 );

vs.

var total = x+2;
someFunction( total );

And how about functions?

someFunction( someOtherFunction() );

Upvotes: 1

Views: 129

Answers (4)

macron
macron

Reputation: 1826

Creating a local variable whose scope does not extend beyond the current function does not incur any cost compared to not creating one and writing the expression directly as the argument to the function. In fact, nothing tells you that writing someFunction(x*2) won't be transformed to code that binds a variable to the result of x*2 internally by the javascript compiler - indeed, many compilers and JITs use SSA as one of their intermediate representations, in which form a variable is always bound to the result of every sub expression. See the relevant Wikipedia entry. Whether in a closure or not makes no difference.

The only two relevant questions that should concern you to make a choice between introducing a new variable and writing the expression as an argument directly are:

  1. Readability: does naming the result of the expression make clearer what the expression is computing;

  2. Cost of evaluating the expression: if you will be writing the expression more than once, then binding a variable to the result will you to reuse avoid recomputing the result everytime. This is only relevant if your expression is expected to take a long time to compute.

If you only need to write the expression once inside a function definition then binding a variable to the result may well make the result live in memory longer than is strictly necessary, but this is nearly always completely irrelevant: most function calls are very short lived, in many cases the result does not take up much memory and the memory allocated on the stack will be reclaimed upon function exit and the memory allocated on the heap will be reclaimed by garbage collector soon thereafter.

Upvotes: 0

Ed Bayiates
Ed Bayiates

Reputation: 11210

Though the difference is minimal, the answer is really implementation-specific; JavaScript engines almost certainly differ in how they allocate things. However, I can tell you that most likely, the differences are similar to what they would be in most other languages of which I can examine the memory and processor registers in the debugger. Let's examine one scenario:

var sum = x+2;
someFunction(sum);

This allocates memory to hold sum, which hangs around as long as the function is in scope. If the function ends up being a closure, this could be forever. In a recursive function this could be significant.

someFunction(x+2);

In most languages, this will compute x+2 on the stack and pass the result to someFunction. No memory is left hanging around.

The answer would be exactly the same for a function return value.

So in summary:

  1. The exact answer depends on the JavaScript engine's implementation.

  2. Most likely you won't notice a performance difference.

  3. You may want to use variables when the result is re-used, or, when you want to examine the result easily in the debugger.

It's mostly a matter of personal preference.

Upvotes: 1

Bergi
Bergi

Reputation: 664307

Just the obvious: Making a variable creates a variable. This costs memory and consumes some time when executing. Afterwards, it either will need time to garbage collect it, or not free the memory if your function leaks.

However, you won't notice any differences. The performance is not measurable at that level. Rule of thumb: Use variables when you really need them or when they improve readabilty of your code.

Upvotes: 2

Wayne
Wayne

Reputation: 60414

No. And, more important, this sort of micro-optimization is (almost certainly) meaningless.

Having said that, if you were to use the result of the expression more than once, then there might be some completely imperceptible and totally not-worth-worrying-about benefit to saving the result of the calculation.

Write it to be readable. Don't worry about this stuff.

Upvotes: 2

Related Questions