Prototype chain (v8)

Revision 8 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 array = [...typed];
var acc = 0;
function getVar(x) {
  return {
    a: array[x],
    b: array[x + 1],
    c: array[x + 2],
    d: array[x + 3],
  };
}
function getRef(array, x) {
  return {
    a: array[x],
    b: array[x + 1],
    c: array[x + 2],
    d: array[x + 3],
  };
}
class Base {
  array;
  constructor(array) {
    this.array = array;
  }
  getThis(x) {
    return {
      a: this.array[x],
      b: this.array[x + 1],
      c: this.array[x + 2],
      d: this.array[x + 3],
    };
  }
  getRef(array, x) {
    return {
      a: array[x],
      b: array[x + 1],
      c: array[x + 2],
      d: array[x + 3],
    };
  }
  static getStaticVar(x) {
    return {
      a: array[x],
      b: array[x + 1],
      c: array[x + 2],
      d: array[x + 3],
    };
  }
  static getStaticRef(array, x) {
    return {
      a: array[x],
      b: array[x + 1],
      c: array[x + 2],
      d: array[x + 3],
    };
  }
}
var base = new Base(array);
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 {
  childThis(x) {
    return {
      a: this.array[x],
      b: this.array[x + 1],
      c: this.array[x + 2],
      d: this.array[x + 3],
    };
  }
  childSuper(x) {
    return super.getThis(x);
  }
}
var child = new Child(array);

Test runner

Ready to run.

Testing in
TestOps/sec
function getVar(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = getVar(x);
  acc += a + b + c + d;
}
ready
base.getThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = base.getThis(x);
  acc += a + b + c + d;
}
ready
child.getThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = child.getThis(x);
  acc += a + b + c + d;
}
ready
child.childThis(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = child.childThis(x);
  acc += a + b + c + d;
}
ready
child.childSuper(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = child.childSuper(x);
  acc += a + b + c + d;
}
ready
function getRef(array, x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = getRef(array, x);
  acc += a + b + c + d;
}
ready
base.getRef(array, x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = base.getRef(array, x);
  acc += a + b + c + d;
}
ready
child.getRef(array, x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = child.getRef(array, x);
  acc += a + b + c + d;
}
ready
Base.getStaticVar(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = Base.getStaticVar(x);
  acc += a + b + c + d;
}
ready
Child.getStaticVar(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = Child.getStaticVar(x);
  acc += a + b + c + d;
}
ready
Base.getStaticRef(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = Base.getStaticRef(array, x);
  acc += a + b + c + d;
}
ready
Child.getStaticRef(x)
for (let x = 0; x < 16384; x += 4) {
  let { a, b, c, d } = Child.getStaticRef(array, 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.