Will Using 'Var' Affect Performance

Will using 'var' affect performance?

There's no extra Intermediate language (IL) code for the var keyword: the resulting IL should be identical for non-anonymous types. If the compiler can't create that IL because it can't figure out what type you intended to use, you'll get a compiler error.

The only trick is that var will infer an exact type where you may have chosen an Interface or parent type if you were to set the type manually.

Does storing in variables increase performance? And do var, let, const also affect performance?

No, simply by storing a value in a variable does not increase the performance, in fact, there will be a slightly reduced performance since additional memory allocation and fetching happens.

However, storing the result of a computation in a variable and using the variable instead of re-computing it every time the values are required improves the performance.

Converting app.listen(3000) into below format does not have any performance benefit.

const port = 3000;
app.listen(port)

However, if the port values require some computation like,

const port = process.env.PORT || 3000;

then storing the result in a variable and using that values every time port number is required will have a performance benefit compared to computing it every time.

(In your example, since the port value is used only once, there is no point in storing the result in a variable.)

I heard, for example, it improves performance of loops.
Again, you have a performance benefit only if the value stored in the variable requires some form of computation.

const array = [1,2,3];
const length = array.length;
for (let i =0 ;i<length; i++) {
console.log(array[i]);
}

The above example has performance benefits compared to the one below.

const array = [1,2,3];
for (let i =0 ;i<array.length; i++) {
console.log(array[i]);
}

In this example, in every iteration of the loop, the length of the array has to be calculated which is not necessary since the array does not change.

And off-topic: isn't app.listen() function kinda a loop? Or does it use an inbuilt loop?

No, app.listen() is not a loop. It is an event listener. Internally the node event loop handles such I/O operations. You can read more about the event loop here.

Is there a performance hit in using the 'var' keyword in C#?

Someone asked that question on here already. In short, no, the IL code generated is the same. Therefore, no perf hit. If you ever wonder how one way of doing something differs from another, just write a sample and then use Reflector to analyze the differences.

How much impact does use of 'var' have on performance of C# Compiler?

My advice: try it both ways. Measure the results. Then you'll know.

I haven't done any benchmarks, and even if I had, that wouldn't answer the question for you. We do not know what hardware you have, what else is running on your machine, what a typical program looks like. Nor do we know what you consider to be acceptable or unacceptable performance. You're the only one who knows all that, so you're the only one who can answer this question.

Is using var actually slow? If so, why?

You state:

I am referring to slow time for compile due to type 'inferencing'

This does not slow down the compiler. The compiler already has to know the result type of the expression, in order to check compatibility (direct or indirect) of the assignment. In some ways using this already-known type removes a few things (the potential to have to check for inheritance, interfaces and conversion operators, for example).

It also doesn't slow down the runtime; they are fully static compiled like regular c# variables (which they are).

In short... it doesn't.

Keyword var performance for compilation

In my experience, any difference is negligible to unobservable. I can find no difference whatsoever, even on larger projects. Whatever difference var makes is statistically insignificant, and lost amid the rest of the compilation work.

C# declaring variables using var vs type

No, there are no performance differences at runtime. var is changed at compile time to the appropriate type.

As for how good of a practice it is: I usually teach my students to use it when the type of the left hand side is clear from the right hand side. Such as:

List<int> myList=new List<int>();

Here it is clear from the right side, what the left side is. Same goes for factory-like methods.
What I don't like is when you have a method like this:

private static int DoSomethingAwesome() {...}

And then it is called like

var x = DoSomethingAwesome();

See how you cannot tell the type of the left side just by reading the code (the return type is not indicated any way whatsoever)? But I guess this might be a little up to taste or opinion.



Related Topics



Leave a reply



Submit