Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: ES6 class vs function benchmark #11609

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions benchmark/es/class-bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

const common = require('../common.js');
const util = require('util');

const A = Symbol('a');
const B = Symbol('b');

const bench = common.createBenchmark(main, {
type: ['class',
'function',
'function-new',
'function-new-instanceof',
'function-new-target'],
millions: [10]
});

class FooBase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a bit easier to read if these are ES6BaseClass, ES6Subclass, FunctionWithNewCheck...(not necessary to be so verbose, just not Foo Bar)

constructor(a) {
this[A] = a;
}

test(a, b, c) {}

get a() {
return this[A];
}
}

class Foo extends FooBase {
constructor(a, b, c) {
super(a);
this[B] = b;
this.c = c;
}

test2(a, b, c) {
}

test3() {}

get b() {
return this[B];
}
}

function BarBase(a, newcheck) {
if (newcheck && !(this instanceof BarBase))
return new BarBase(a);
this[A] = a;
}
BarBase.prototype.test = function(a, b, c) {};

Object.defineProperty(BarBase.prototype, 'a', {
get: function() { return this[A]; }
});

function Bar(a, b, c, newcheck) {
if (newcheck && !(this instanceof Bar))
return new Bar(a, b, c);
BarBase.call(this, a);
this[B] = b;
this.c = c;
}
util.inherits(Bar, BarBase);
Bar.prototype.test2 = function(a, b, c) {};
Bar.prototype.test3 = function() {};

Object.defineProperty(Bar.prototype, 'b', {
get: function() { return this[B]; }
});

function BazBase(a, newcheck) {
if (new.target === undefined)
return new BazBase(a);
this[A] = a;
}
BazBase.prototype.test = function(a, b, c) {};

Object.defineProperty(BazBase.prototype, 'a', {
get: function() { return this[A]; }
});

function Baz(a, b, c) {
if (new.target === undefined)
return new Baz(a, b, c);
BazBase.call(this, a);
this[B] = b;
this.c = c;
}
util.inherits(Baz, BazBase);
Baz.prototype.test2 = function(a, b, c) {};
Baz.prototype.test3 = function() {};

Object.defineProperty(Baz.prototype, 'b', {
get: function() { return this[B]; }
});

function runClass(n, obj) {
var i;
bench.start();
for (i = 0; i < n; i++)
exercise(new Foo(1, 2, 3));
bench.end(n / 1e6);
}

function runFunctionWithNew(n, newcheck) {
var i;
bench.start();
for (i = 0; i < n; i++)
exercise(new Bar(1, 2, 3, newcheck));
bench.end(n / 1e6);
}

function runFunctionWithoutNew(n, newcheck) {
var i;
bench.start();
for (i = 0; i < n; i++)
exercise(Bar(1, 2, 3, true))
bench.end(n / 1e6);
}

function runFunctionWithNewTarget(n) {
var i;
bench.start();
for (i = 0; i < n; i++)
exercise(new Baz(1, 2, 3));
bench.end(n / 1e6);
}

function exercise(obj) {
obj.test();
obj.test2();
obj.test3();
obj.a;
obj.b;
obj.c;
}

function main(conf) {
const n = +conf.millions * 1e6;

switch (conf.type) {
case 'class':
runClass(n);
break;
case 'function':
runFunctionWithoutNew(n);
break;
case 'function-new':
runFunctionWithNew(n);
break;
case 'function-new-instanceof':
runFunctionWithNew(n, true);
break;
case 'function-new-target':
runFunctionWithNewTarget(n);
break;
default:
throw new Error('Unexpected type');
}
}