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: util._extend vs object.assign #7255

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions benchmark/misc/util-extend-vs-object-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

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

const bench = common.createBenchmark(main, {
type: ['util._extend', 'Object.assign',
Copy link
Contributor

@mscdex mscdex Jun 10, 2016

Choose a reason for hiding this comment

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

I think these can be shortened down to just extend (or _extend) and assign.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

'util._extend', 'Object.assign'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are there duplicates?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

n: [10e4]
});

function main(params) {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: it's probably better to keep with the same variable naming as other benchmarks, in other words, s/params/conf/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

let fn;
const n = params.n | 0;
let v8command;

if (params.type == 'util._extend') {
fn = util._extend;
v8command = '%OptimizeFunctionOnNextCall(util._extend)';
Copy link
Contributor

@mscdex mscdex Jun 10, 2016

Choose a reason for hiding this comment

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

I don't know exactly how %OptimizeFunctionOnNextCall() works/is implemented, but I wonder if util._extend here should instead be fn?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is the right way. It will get eval to the correct function reference by v8.


} else if (params.type == 'Object.assign') {
fn = Object.assign;
//Object.assign is built-in, cannot be optimized
Copy link
Contributor

Choose a reason for hiding this comment

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

Space needed after //.

v8command = '';
}

// Force-optimize the method to test so that the benchmark doesn't
// get disrupted by the optimizer kicking in halfway through.
for (let i = 0; i < params.type.length * 10; i += 1)
fn({}, process.env);

v8.setFlagsFromString('--allow_natives_syntax');
eval(v8command);

bench.start();
for (let i = 0; i < n; i += 1)
fn({}, process.env);
Copy link
Contributor

@mscdex mscdex Jun 10, 2016

Choose a reason for hiding this comment

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

I'm not sure if this is the best test or not. At the very least, how does this compare with just using a same, empty, frozen object instead of a new {} every time? For example:

var obj = {};
Object.freeze(obj);

bench.start();
for (var i = 0; i < n; i += 1)
  fn(obj, process.env);
bench.end(n);

Also, on an unrelated note, it's best to avoid using let in loops like that until v8 optimizes that use case. It probably matters less on benchmarks like this, but hey, the sooner the benchmark finishes the better, right? ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both assign and _extend would throw exception if the first argument is frozen, since they modify it. However, used a pre-defined obj as you recommended above.

Copy link
Contributor

@mscdex mscdex Jun 11, 2016

Choose a reason for hiding this comment

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

Ok, how about using a Proxy then?:

var obj = new Proxy({}, { set: function(a, b, c) { return true; } });

bench.start();
for (var i = 0; i < n; i += 1)
  fn(obj, process.env);
bench.end(n);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

bench.end(n);
}