Benchmark of Javascript Function vs Class usage (v3)

Revision 3 of this benchmark created on


Description

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Setup

var arr = [];

Test runner

Ready to run.

Testing in
TestOps/sec
Function
function TestFunction(item) {
  this.item = item;
  this.addItem = function() {
    arr.push(this.item);
  }
}
for (let i = 1; i < 100000; i++) {
  new TestFunction(i).addItem();
}
ready
Class
class TestClass {
  item;
  constructor(item) {
    this.item = item;
  }
  addItem() {
    arr.push(this.item);
  }
}
for (let i = 1; i < 100000; i++) {
  new TestClass(i).addItem();
}
ready
Class - Uninitialised Field
class TestClassUninitialised {
  constructor(item) {
    this.item = item;
  }
  addItem() {
    arr.push(this.item);
  }
}
for (let i = 1; i < 100000; i++) {
  new TestClassUninitialised(i).addItem();
}
ready

Revisions

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