Al.
Al.

Reputation: 875

local variable performance in Javascript vs C#

My Javascript code here. Instead of everytime trying to access document which is global context we made it to activation object. So that we can improve our read/write performance.

function initUI(){
        var doc = document,
            bd = doc.body,
            links = doc.getElementsByTagName("a"),
            i= 0,
            len = links.length;
        while(i < len){
            update(links[i++]);
        }
        doc.getElementById("go-btn").onclick = function(){
            start();
        };
        bd.className = "active";
    }

Whether is it applicable to C# as well? Lets say,

defining var customObject = new CustomClass(); as a member variable and accessing like below,

void MyMethod()
{
var obj = customObject;
var name = obj.name;
//some code here
..
..
}

will increase the performance?

Upvotes: 0

Views: 186

Answers (1)

Florian Margaine
Florian Margaine

Reputation: 60747

No, it's not the same in C#.

In javascript, you cache the DOM objects, because the DOM is awfully slow to read/write.

You don't need to cache normal objects properties. If you do it, it is to gain some characters, but not for performance.

In C#, you don't have to deal with the DOM. You don't need to cache the objects, except if you want to gain some characters.

Upvotes: 2

Related Questions