Skip to content
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
benchmark: obj property access benchmark
Benchmark that compares the obj property access patterns being
used in core today. Specifically comparing the two patterns:

```js
function A() {};
function B() {};
var obj = {A, B, C: 1};
```

and

```js
function A() {}
function B() {}
var obj = {};
obj.A = A;
obj.B = B;
obj.C = 1;
```
  • Loading branch information
jasnell committed Feb 28, 2017
commit 22450b5f28770ee330b019fd92b63cda5dedd3f2
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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;