简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM