Al W
Al W

Reputation: 476

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?

Upvotes: 1

Views: 94

Answers (1)

Alxandr
Alxandr

Reputation: 12423

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.

Upvotes: 3

Related Questions