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: add benchmark for object properties #10949

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
81 changes: 81 additions & 0 deletions benchmark/misc/object-property-bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

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

const bench = common.createBenchmark(main, {
method: ['property', 'string', 'variable', 'symbol'],
millions: [1000]
});

function runProperty(n) {
const object = {};
var i = 0;
bench.start();
for (; i < n; i++) {
object.p1 = 21;
object.p2 = 21;
object.p1 += object.p2;
}
bench.end(n / 1e6);
}

function runString(n) {
const object = {};
var i = 0;
bench.start();
for (; i < n; i++) {
object['p1'] = 21;
object['p2'] = 21;
object['p1'] += object['p2'];
}
bench.end(n / 1e6);
}

function runVariable(n) {
const object = {};
const var1 = 'p1';
const var2 = 'p2';
var i = 0;
bench.start();
for (; i < n; i++) {
object[var1] = 21;
object[var2] = 21;
object[var1] += object[var2];
}
bench.end(n / 1e6);
}

function runSymbol(n) {
const object = {};
const symbol1 = Symbol('p1');
const symbol2 = Symbol('p2');
var i = 0;
bench.start();
for (; i < n; i++) {
object[symbol1] = 21;
object[symbol2] = 21;
object[symbol1] += object[symbol2];
}
bench.end(n / 1e6);
}

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

switch (conf.method) {
case 'property':
runProperty(n);
break;
case 'string':
runString(n);
break;
case 'variable':
runVariable(n);
break;
case 'symbol':
runSymbol(n);
break;
default:
throw new Error('Unexpected method');
}
}