I read you could simulate static local variables in js like this:
function count() {
count.i++;
}
count.i = 0;
Is storing ‘i’ as a property of ‘count’ faster than using a global? eg.
var i=0;
function count ()
{i++;
}
Just a performance comparison question.
Thanks.
Seems an externally stored value is faster. See this test. It matters if you assign
count.iwithin the function or outside. Assigning it outside the function shows a small difference. I wouldn’t worry about that.You can also consider this to emulate something static (added to the jsperf-test, it’s roughly as fast as assigning a global variable or an externally assigned
count.i)