Search code examples
javascriptperformanceprototypeobject-literal

Javascript performance of accessing object's fields if object was prototyped and if it was created from a literal


I can create object using prototype, and the fields are set in the constructor, or I can create object using JSON. I'd expect that the prototyped version will be as fast as the literal, or faster, but it occurs that it's slower on chrome and ff, while on Opera both seem to be equal.

http://jsperf.com/object-literal-vs-object-prototype-field-access-time

Can someone explain it?


Solution

  • AFAIK prototype access is basically just 2 normal accesses (except that access to the prototype is highly optimized). Writing this.test is basically the same as writing

    if(this.hasOwnProperty('test')) { return test; }
    else { return this.constructor.prototype['test']; }
    

    Though, I'm not 100% sure of this.