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

benchmarks: add spread operator benchmark #11227

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

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

const bench = common.createBenchmark(main, {
method: ['apply', 'spread', 'call-spread'],
count: [5, 10, 20],
context: ['context', 'null'],
rest: [0, 1],
millions: [5]
});

function makeTest(count, rest) {
if (rest) {
return function test(...args) {
assert.strictEqual(count, args.length);
};
} else {
return function test() {
assert.strictEqual(count, arguments.length);
};
}
}

function main(conf) {
const n = +conf.millions * 1e6;
const ctx = conf.context === 'context' ? {} : null;
var fn = makeTest(conf.count, conf.rest);
Copy link
Contributor

@mscdex mscdex Feb 10, 2017

Choose a reason for hiding this comment

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

I think the linter may be complaining about this, that it should be const. Might want to run it locally just to be sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

O.o .. nope, just a missing ; ... fixed!

const args = new Array(conf.count);
var i;
for (i = 0; i < conf.count; i++)
args[i] = i;

switch (conf.method) {
case 'apply':
bench.start();
for (i = 0; i < n; i++)
fn.apply(ctx, args);
bench.end(n / 1e6);
break;
case 'spread':
if (ctx !== null)
fn = fn.bind(ctx);
bench.start();
for (i = 0; i < n; i++)
fn(...args);
bench.end(n / 1e6);
break;
case 'call-spread':
bench.start();
for (i = 0; i < n; i++)
fn.call(ctx, ...args);
bench.end(n / 1e6);
break;
default:
throw new Error('Unexpected method');
}
}