Prototype chain (v6)

Revision 6 of this benchmark created on


Description

Does extending a class add method call overhead?

Setup

var size = 16384;
var typed = new Int32Array(size);
crypto.getRandomValues(typed);
var acc = 0;
function retrieveProp(x) {
  return {
    a: typed[x],
    b: typed[x+1],
    c: typed[x+2],
    d: typed[x+3],
  };
}
function retrievePropRef(array, x) {
  return {
    a: array[x],
    b: array[x+1],
    c: array[x+2],
    d: array[x+3],
  };
}
class Base {
  values;
  constructor(values) {
    this.values = values;
  }
  retrieveProp(x) {
  	return {
      a: this.values[x],
      b: this.values[x+1],
      c: this.values[x+2],
      d: this.values[x+3],
    };
  }
  retrieveDirect(array, x) {
  	return {
      a: array[x],
      b: array[x+1],
      c: array[x+2],
      d: array[x+3],
    };  	
  }
  static retrieveStatic(array, x) {
  	return {
      a: array[x],
      b: array[x+1],
      c: array[x+2],
      d: array[x+3],
    };  	
  }
}
var base = new Base(typed);
class Base1 extends Base {}
class Base2 extends Base1 {}
class Base3 extends Base2 {}
class Base4 extends Base3 {}
class Base5 extends Base4 {}
class Base6 extends Base5 {}
class Base7 extends Base6 {}
class Base8 extends Base7 {}
class Base9 extends Base8 {}
class BaseA extends Base9 {}
class BaseB extends BaseA {}
class BaseC extends BaseB {}
class BaseD extends BaseC {}
class BaseE extends BaseD {}
class BaseF extends BaseE {}
class Child extends BaseF {
  retrieveChild(x) {
  	return {
      a: this.values[x],
      b: this.values[x+1],
      c: this.values[x+2],
      d: this.values[x+3],
    };
  }
  retrieveProp2(x) {
  	return super.retrieveProp(x);
  }
}
var child = new Child(typed);
function inlineProp(instance, x) {
  return instance.retrieveProp(x);
}

Test runner

Ready to run.

Testing in
TestOps/sec
direct function
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = retrieveProp(x);
	acc += a + b + c + d;
}
ready
Base class
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = base.retrieveProp(x);
	acc += a + b + c + d;
}
ready
Child class, Child method
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = child.retrieveChild(x);
	acc += a + b + c + d;
}
ready
Child class, Base method
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = child.retrieveProp(x);
	acc += a + b + c + d;
}
ready
Child class, Super method
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = child.retrieveProp2(x);
	acc += a + b + c + d;
}
ready
inline Child, Base
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = inlineProp(child, x);
	acc += a + b + c + d;
}
ready
inline Base, Base
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = inlineProp(base, x);
	acc += a + b + c + d;
}
ready
Child class, array ref
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = child.retrieveDirect(typed, x);
	acc += a + b + c + d;
}
ready
Base class, Static method, array ref
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = Base.retrieveStatic(typed, x);
	acc += a + b + c + d;
}
ready
Child class, Static method, array ref
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = Child.retrieveStatic(typed, x);
	acc += a + b + c + d;
}
ready
direct function, array ref
for(let x = 0; x < 16384; x+=4) {
	let {a,b,c,d} = retrievePropRef(typed, x);
	acc += a + b + c + d;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.