forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharguments.js
57 lines (48 loc) · 1.07 KB
/
arguments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const { createBenchmark } = require('../common.js');
const { format } = require('util');
const methods = [
'restAndSpread',
'argumentsAndApply',
'restAndApply',
'predefined',
];
const bench = createBenchmark(main, {
method: methods,
n: [1e6],
});
function usingRestAndSpread(...args) {
format(...args);
}
function usingRestAndApply(...args) {
format.apply(null, args);
}
function usingArgumentsAndApply() {
format.apply(null, arguments);
}
function usingPredefined() {
format('part 1', 'part', 2, 'part 3', 'part', 4);
}
function main({ n, method, args }) {
let fn;
switch (method) {
case 'restAndSpread':
fn = usingRestAndSpread;
break;
case 'restAndApply':
fn = usingRestAndApply;
break;
case 'argumentsAndApply':
fn = usingArgumentsAndApply;
break;
case 'predefined':
fn = usingPredefined;
break;
default:
throw new Error(`Unexpected method "${method}"`);
}
bench.start();
for (let i = 0; i < n; i++)
fn('part 1', 'part', 2, 'part 3', 'part', 4);
bench.end(n);
}