Script Preparation code:
x
 
class Cls {
  constructor(foo, bar, baz) {
    this.foo = foo;
    this.bar = bar;
    this.baz = baz;
  }
  sum() {
    return this.foo + this.bar + this.baz;
  }
}
function Fn(foo, bar, baz) {
  this.foo = foo;
  this.bar = bar;
  this.baz = baz;
}
Fn.prototype.sum = function() {
  return this.foo + this.bar + this.baz;
}
function obj(foo, bar, baz) {
  return {foo, bar, baz};
}
function sum(x) {
  return x.foo + x.bar + x.baz;
}
function sum2() {
  return this.foo + this.bar + this.baz;
}
const proto = {
  sum() {
    return this.foo + this.bar + this.baz;
  }
}
function protoCtr(foo, bar, baz) {
  return {foo, bar, baz, __proto__: proto };
}
function create(foo, bar, baz) {
  let x = Object.create(proto);
  x.foo = foo;
  x.bar = bar;
  x.baz = baz;
  return x;
}
function setproto(foo, bar, baz) {
  let x = {foo, bar, baz};
  Object.setPrototypeOf(x, proto);
  return x;
}
window.Cls = Cls
window.Fn = Fn
window.obj = obj
window.sum = sum
window.sum2 = sum2
window.protoCtr = protoCtr
window.create = create
window.setproto = setproto
Tests:
  • class

     
    const ctr = window.Cls
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = new ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += x.sum();
    }
    return y;
  • function constructor

     
    const ctr = window.Fn;
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = new ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += x.sum();
    }
    return y;
  • object literal

     
    const ctr = window.obj;
    const sum = window.sum;
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += sum(x);
    }
    return y;
  • object literal & sum.call

     
    const ctr = window.obj;
    const sum = window.sum2;
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += sum.call(x);
    }
    return y;
  • __proto__

     
    const ctr = window.protoCtr
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += x.sum();
    }
    return y;
  • Object.create

     
    const ctr = window.create
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += x.sum();
    }
    return y;
  • Object.setPrototypeOf

     
    const ctr = window.setproto
    let i = 1000;
    let y = 0;
    while (i--) {
      let x = ctr(1, 2, 3);
      y += x.foo + x.bar + x.baz;
      y += x.sum();
    }
    return y;
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    class
    function constructor
    object literal
    object literal & sum.call
    __proto__
    Object.create
    Object.setPrototypeOf

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 months ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Chrome 130 on Windows
View result in a separate tab
Test name Executions per second
class 1142340.9 Ops/sec
function constructor 1106223.8 Ops/sec
object literal 969796.2 Ops/sec
object literal & sum.call 1029964.4 Ops/sec
__proto__ 23787.5 Ops/sec
Object.create 8007.3 Ops/sec
Object.setPrototypeOf 5304.3 Ops/sec