Benchmark of Javascript Function vs Class usage

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 {
  constructor(item) {
    this.item = item;
  }
  addItem() {
    arr.push(this.item);
  }
}
for (let i = 1; i < 100000; i++) {
  new TestClass(i).addItem();
}
ready

Revisions

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