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