javascriptperformancepremature-optimization

Getting value from object vs getting value from global value


I know this is premature optimization but I am just curious to know how long does it take to get the value of this

var objects =
{
    number:10
}
console.log(""+objects.number);

VS

var number = 10;
console.log("" + number);

I just do not know how to benchmark so if I knew I would do it myself but if this is a really bad question please do not -rep me, just tell me and I'll remove the question


Solution

  • The results are intensely varying. See this test: http://jsperf.com/property-vs-plain-variable

    Run it many times, you can see the results orbit around, without a definitive result (at least on Firefox). Sometimes accessing a variable is slower than accessing the object's property directly, I think you can assume there is no real difference.