Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does the depth of the prototype chain for an object affect the performance?

Several days ago,I post a question here about class inheritance

Then someone provide a link-- a clever script for class inheritance by John Resig.

Then I try to use this script.

But I found that object created by this script will have a very deep prototype chain.

I use the example in the blog:

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});

var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  }
});

var n=new Ninja(true);
console.dir(n);

With firebug I found this: enter image description here

So I wonder does the depth of the prototype chain for an object affect the performance?

It seem there is a Endless loop.

like image 332
hguser Avatar asked Sep 02 '25 17:09

hguser


1 Answers

The prototype of a constructor is the same constructor. So yes, you are looking at an endless loop.

However browsers don't follow the constructor chain when resolving object's members, it has an internal pointer (sometimes called __PROTO__ but don't count on it) which is actually used. A JavaScript engine might also opt to compile to machine code which resolves the possible members in advance so no chain at all exists during actual execution.

I strongly doubt that you will experience any slow-down at all.

like image 51
clockworkgeek Avatar answered Sep 04 '25 06:09

clockworkgeek