2017-02-08 04:42:04 +08:00
|
|
|
'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],
|
2018-03-17 23:19:09 +08:00
|
|
|
n: [5e6]
|
2017-02-08 04:42:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
function makeTest(count, rest) {
|
|
|
|
if (rest) {
|
|
|
|
return function test(...args) {
|
|
|
|
assert.strictEqual(count, args.length);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return function test() {
|
|
|
|
assert.strictEqual(count, arguments.length);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 23:19:09 +08:00
|
|
|
function main({ n, context, count, rest, method }) {
|
2017-12-30 10:59:27 +08:00
|
|
|
const ctx = context === 'context' ? {} : null;
|
|
|
|
var fn = makeTest(count, rest);
|
|
|
|
const args = new Array(count);
|
2017-02-08 04:42:04 +08:00
|
|
|
var i;
|
2017-12-30 10:59:27 +08:00
|
|
|
for (i = 0; i < count; i++)
|
2017-02-08 04:42:04 +08:00
|
|
|
args[i] = i;
|
|
|
|
|
2017-12-30 10:59:27 +08:00
|
|
|
switch (method) {
|
2017-10-07 00:59:28 +08:00
|
|
|
case '':
|
|
|
|
// Empty string falls through to next line as default, mostly for tests.
|
2017-02-08 04:42:04 +08:00
|
|
|
case 'apply':
|
|
|
|
bench.start();
|
2018-03-17 23:19:09 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2017-02-08 04:42:04 +08:00
|
|
|
fn.apply(ctx, args);
|
2018-03-17 23:19:09 +08:00
|
|
|
bench.end(n);
|
2017-02-08 04:42:04 +08:00
|
|
|
break;
|
|
|
|
case 'spread':
|
|
|
|
if (ctx !== null)
|
|
|
|
fn = fn.bind(ctx);
|
|
|
|
bench.start();
|
2018-03-17 23:19:09 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2017-02-08 04:42:04 +08:00
|
|
|
fn(...args);
|
2018-03-17 23:19:09 +08:00
|
|
|
bench.end(n);
|
2017-02-08 04:42:04 +08:00
|
|
|
break;
|
|
|
|
case 'call-spread':
|
|
|
|
bench.start();
|
2018-03-17 23:19:09 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2017-02-08 04:42:04 +08:00
|
|
|
fn.call(ctx, ...args);
|
2018-03-17 23:19:09 +08:00
|
|
|
bench.end(n);
|
2017-02-08 04:42:04 +08:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-23 20:18:30 +08:00
|
|
|
throw new Error(`Unexpected method "${method}"`);
|
2017-02-08 04:42:04 +08:00
|
|
|
}
|
|
|
|
}
|