Prototype chain

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],
  };
}
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],
    };
  }
}
var base = new Base(typed);
class Child extends Base {
  retrieveChild(x) {
  	return {
      a: this.values[x],
      b: this.values[x+1],
      c: this.values[x+2],
      d: this.values[x+3],
    };
  }
}
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
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

Revisions

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