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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
method: ['copy', 'rest', 'arguments'],
n: [1e8]
});
function copyArguments() {
const len = arguments.length;
const args = new Array(len);
for (var i = 0; i < len; i++)
args[i] = arguments[i];
assert.strictEqual(args[0], 1);
assert.strictEqual(args[1], 2);
assert.strictEqual(args[2], 'a');
assert.strictEqual(args[3], 'b');
}
function restArguments(...args) {
assert.strictEqual(args[0], 1);
assert.strictEqual(args[1], 2);
assert.strictEqual(args[2], 'a');
assert.strictEqual(args[3], 'b');
}
function useArguments() {
assert.strictEqual(arguments[0], 1);
assert.strictEqual(arguments[1], 2);
assert.strictEqual(arguments[2], 'a');
assert.strictEqual(arguments[3], 'b');
}
function runCopyArguments(n) {
for (var i = 0; i < n; i++)
copyArguments(1, 2, 'a', 'b');
}
function runRestArguments(n) {
for (var i = 0; i < n; i++)
restArguments(1, 2, 'a', 'b');
}
function runUseArguments(n) {
for (var i = 0; i < n; i++)
useArguments(1, 2, 'a', 'b');
}
function main({ n, method }) {
var fn;
switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'copy':
fn = runCopyArguments;
break;
case 'rest':
fn = runRestArguments;
break;
case 'arguments':
fn = runUseArguments;
break;
default:
throw new Error(`Unexpected method "${method}"`);
}
bench.start();
fn(n);
bench.end(n);
}
|