|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const assert = require('assert'); |
| 5 | +const Writable = require('stream').Writable; |
| 6 | +const util = require('util'); |
| 7 | +const v8 = require('v8'); |
| 8 | + |
| 9 | +v8.setFlagsFromString('--allow_natives_syntax'); |
| 10 | + |
| 11 | +var bench = common.createBenchmark(main, { |
| 12 | + method: ['restAndSpread', |
| 13 | + 'argumentsAndApply', |
| 14 | + 'restAndApply', |
| 15 | + 'restAndConcat'], |
| 16 | + concat: [1, 0], |
| 17 | + n: [1000000] |
| 18 | +}); |
| 19 | + |
| 20 | +const nullStream = createNullStream(); |
| 21 | + |
| 22 | +function usingRestAndConcat(...args) { |
| 23 | + nullStream.write('this is ' + args[0] + ' of ' + args[1] + '\n'); |
| 24 | +} |
| 25 | + |
| 26 | +function usingRestAndSpreadTS(...args) { |
| 27 | + nullStream.write(`${util.format(...args)}\n`); |
| 28 | +} |
| 29 | + |
| 30 | +function usingRestAndApplyTS(...args) { |
| 31 | + nullStream.write(`${util.format.apply(null, args)}\n`); |
| 32 | +} |
| 33 | + |
| 34 | +function usingArgumentsAndApplyTS() { |
| 35 | + nullStream.write(`${util.format.apply(null, arguments)}\n`); |
| 36 | +} |
| 37 | + |
| 38 | +function usingRestAndSpreadC(...args) { |
| 39 | + nullStream.write(util.format(...args) + '\n'); |
| 40 | +} |
| 41 | + |
| 42 | +function usingRestAndApplyC(...args) { |
| 43 | + nullStream.write(util.format.apply(null, args) + '\n'); |
| 44 | +} |
| 45 | + |
| 46 | +function usingArgumentsAndApplyC() { |
| 47 | + nullStream.write(util.format.apply(null, arguments) + '\n'); |
| 48 | +} |
| 49 | + |
| 50 | +function optimize(method, ...args) { |
| 51 | + method(...args); |
| 52 | + eval(`%OptimizeFunctionOnNextCall(${method.name})`); |
| 53 | + method(...args); |
| 54 | +} |
| 55 | + |
| 56 | +function runUsingRestAndConcat(n) { |
| 57 | + optimize(usingRestAndConcat, 'a', 1); |
| 58 | + |
| 59 | + var i = 0; |
| 60 | + bench.start(); |
| 61 | + for (; i < n; i++) |
| 62 | + usingRestAndConcat('a', 1); |
| 63 | + bench.end(n); |
| 64 | +} |
| 65 | + |
| 66 | +function runUsingRestAndSpread(n, concat) { |
| 67 | + |
| 68 | + const method = concat ? usingRestAndSpreadC : usingRestAndSpreadTS; |
| 69 | + optimize(method, 'this is %s of %d', 'a', 1); |
| 70 | + |
| 71 | + var i = 0; |
| 72 | + bench.start(); |
| 73 | + for (; i < n; i++) |
| 74 | + method('this is %s of %d', 'a', 1); |
| 75 | + bench.end(n); |
| 76 | +} |
| 77 | + |
| 78 | +function runUsingRestAndApply(n, concat) { |
| 79 | + |
| 80 | + const method = concat ? usingRestAndApplyC : usingRestAndApplyTS; |
| 81 | + optimize(method, 'this is %s of %d', 'a', 1); |
| 82 | + |
| 83 | + var i = 0; |
| 84 | + bench.start(); |
| 85 | + for (; i < n; i++) |
| 86 | + method('this is %s of %d', 'a', 1); |
| 87 | + bench.end(n); |
| 88 | +} |
| 89 | + |
| 90 | +function runUsingArgumentsAndApply(n, concat) { |
| 91 | + |
| 92 | + const method = concat ? usingArgumentsAndApplyC : usingArgumentsAndApplyTS; |
| 93 | + optimize(method, 'this is %s of %d', 'a', 1); |
| 94 | + |
| 95 | + var i = 0; |
| 96 | + bench.start(); |
| 97 | + for (; i < n; i++) |
| 98 | + method('this is %s of %d', 'a', 1); |
| 99 | + bench.end(n); |
| 100 | +} |
| 101 | + |
| 102 | +function main(conf) { |
| 103 | + const n = +conf.n; |
| 104 | + switch (conf.method) { |
| 105 | + case 'restAndSpread': |
| 106 | + runUsingRestAndSpread(n, conf.concat); |
| 107 | + break; |
| 108 | + case 'restAndApply': |
| 109 | + runUsingRestAndApply(n, conf.concat); |
| 110 | + break; |
| 111 | + case 'argumentsAndApply': |
| 112 | + runUsingArgumentsAndApply(n, conf.concat); |
| 113 | + break; |
| 114 | + case 'restAndConcat': |
| 115 | + if (conf.concat) |
| 116 | + runUsingRestAndConcat(n); |
| 117 | + break; |
| 118 | + default: |
| 119 | + throw new Error('Unexpected method'); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function createNullStream() { |
| 124 | + // Used to approximate /dev/null |
| 125 | + function NullStream() { |
| 126 | + Writable.call(this, {}); |
| 127 | + } |
| 128 | + util.inherits(NullStream, Writable); |
| 129 | + NullStream.prototype._write = function(cb) { |
| 130 | + assert.strictEqual(cb.toString(), 'this is a of 1\n'); |
| 131 | + }; |
| 132 | + return new NullStream(); |
| 133 | +} |
0 commit comments