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: obj property access benchmark #11611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
53 changes: 53 additions & 0 deletions benchmark/es/obj-bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const common = require('../common.js');
const Obj1 = require('../fixtures/moduleA');
const Obj2 = require('../fixtures/moduleB');

const bench = common.createBenchmark(main, {
// TODO(jasnell): A and B are not great labels, not sure what would be better
method: ['A', 'B'],
access: ['direct', 'indirect'],
millions: [10]
});

function run(n, obj, direct) {
var i;
if (direct) {
bench.start();
for (i = 0; i < n; i++) {
obj.A();
obj.B();
obj.C;
}
bench.end(n / 1e6);
} else {
const A = obj.A;
const B = obj.B;
bench.start();
for (i = 0; i < n; i++) {
A();
B();
obj.C;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why have this if it's the same in both direct/indirect cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

Which specifically? The obj.C bit? Basically checking any accumulative effect. It may not make sense to keep tho :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

.. by accumulative effect, I mean I want to measure the performance of all the accesses on the different module patterns.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the obj.C is what I'm referring to.

}
bench.end(n / 1e6);
}
}

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

switch (conf.method) {
case 'A':
// Uses the {A, B, C: 1}; Syntax
run(n, Obj1, direct);
break;
case 'B':
// Uses the obj = {}; obj.A = A; obj.B = B; obj.C = 1; Syntax
run(n, Obj2, direct);
break;
default:
throw new Error('Unexpected type');
}
}
11 changes: 11 additions & 0 deletions benchmark/fixtures/moduleA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

function A() {}

function B() {}

module.exports = {
A,
B,
C: 1
};
7 changes: 7 additions & 0 deletions benchmark/fixtures/moduleB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

exports.A = function() {};

exports.B = function() {};

exports.C = 1;