How memory is allocated in a for (prop in obj) loop and the best approach

I have the following snippet

for(var prop in windowManager.owners) {
    if(windowManager.owners.hasOwnProperty(prop)) {
        if(windowManager.owners[prop] instanceof WhatAmILookingFor) {
            if(windowManager.owners[prop].id=== lookupid) {
                return windowManager.owners[prop];
            }
        }
    }
}

It would be better to keep the reference to windowManager.owners[prop]in the temp variable, how is it?

var win = null;
for(var prop in windowManager.owners) {
    if(windowManager.owners.hasOwnProperty(prop)) {
        win = windowManager.owners[prop];
        if(win instanceof WhatAmILookingFor) {
            if(win.id === lookupid) {
                return win;
            }
        }
    }
}

Are there any advantages to this? Will there be overhead by doing so? Or will this not affect gc as the object winwill be reused?

What would be the best course of action?

I really understand that we are talking about microseconds here in speed difference, I'm just interested in behind-the-scenes actions, for example, how memory is allocated / reused and what would be the best use of reasonable memory.

+4
source share
1 answer

, .

windowManager.owners[prop]

" windowManager , owners, prop ". - , JavaScript , .

- . , . , - prop prop2 1 3 - .

, , , , , .

, . win , (= ). . null , , .

+2

All Articles