From 8796cb1a3a523273c5063f782214b7dd054d3f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=B2=E8=80=98=E2=84=A2?= Date: Mon, 31 Aug 2015 10:36:11 +0800 Subject: [PATCH] refact(apply): apply is not too slow than direct call. fixed #2 --- benchmark/apply.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 18 ++--------- 2 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 benchmark/apply.js diff --git a/benchmark/apply.js b/benchmark/apply.js new file mode 100644 index 0000000..0682a93 --- /dev/null +++ b/benchmark/apply.js @@ -0,0 +1,80 @@ +var Benchmark = require('benchmark'); +var benchmarks = require('beautify-benchmark'); +var util = require('util'); + +var suite = new Benchmark.Suite(); + +function normal(text) { + if (arguments.length === 2) { + return util.format(text, arguments[1]); + } else if (arguments.length === 3) { + return util.format(text, arguments[1], arguments[2]); + } else if (arguments.length === 4) { + return util.format(text, arguments[1], arguments[2], arguments[3]); + } else if (arguments.length === 5) { + return util.format(text, arguments[1], arguments[2], arguments[3], arguments[4]); + } +} + +function apply() { + var args = Array.prototype.slice.call(arguments); + return util.format.apply(util, args); +} + +function apply2() { + var args = new Array(arguments.length); + for (var i = 0, l = arguments.length; i < l; i++) { + args[i] = arguments[i]; + } + return util.format.apply(util, args); +} + +console.log('normal(): %s', normal('this is %s.', 'string')); +console.log('apply(): %s', apply('this is %s.', 'string')); +console.log('apply2(): %s', apply2('this is %s.', 'string')); + +suite + +.add('normal(arg0, arg1, ...)', function() { + normal('this is %s.', 'string'); + normal('this is %s and %s.', 'string', 'string2'); + normal('this is %s and %s and %s.', 'string', 'string2', 'string3'); + normal('this is %s and %s and %s and %s.', 'string', 'string2', 'string3', 'string4'); +}) +.add('function.apply(arg0, arg1, ...)', function() { + apply('this is %s.', 'string'); + apply('this is %s and %s.', 'string', 'string2'); + apply('this is %s and %s and %s.', 'string', 'string2', 'string3'); + apply('this is %s and %s and %s and %s.', 'string', 'string2', 'string3', 'string4'); +}) +.add('function.apply2(arg0, arg1, ...)', function() { + apply2('this is %s.', 'string'); + apply2('this is %s and %s.', 'string', 'string2'); + apply2('this is %s and %s and %s.', 'string', 'string2', 'string3'); + apply2('this is %s and %s and %s and %s.', 'string', 'string2', 'string3', 'string4'); +}) + +.on('cycle', function(event) { + benchmarks.add(event.target); +}) +.on('start', function() { + console.log('\n dynamic arguments Benchmark\n node version: %s, date: %s\n Starting...', + process.version, Date()); +}) +.on('complete', function done() { + benchmarks.log(); +}) +.run({ async: false }); + +// normal(): this is string. +// apply(): this is string. +// apply2(): this is string. +// +// dynamic arguments Benchmark +// node version: v2.2.1, date: Sun Aug 30 2015 20:45:42 GMT+0800 (CST) +// Starting... +// 3 tests completed. +// +// normal(arg0, arg1, ...) x 273,983 ops/sec ±0.80% (101 runs sampled) +// function.apply(arg0, arg1, ...) x 222,616 ops/sec ±0.70% (98 runs sampled) +// function.apply2(arg0, arg1, ...) x 265,349 diff --git a/index.js b/index.js index d6a2f00..d17f039 100644 --- a/index.js +++ b/index.js @@ -77,7 +77,7 @@ module.exports = function (app, options) { return Object.prototype.toString.call(obj) === '[object Object]'; } - app.context[functionName] = function (key, value, value2, value3, value4) { + app.context[functionName] = function (key, value) { if (arguments.length === 0) { // __() return ''; @@ -91,8 +91,6 @@ module.exports = function (app, options) { return ''; } - // for performance reason - // https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments if (arguments.length === 1) { // __(key) return text; @@ -117,20 +115,8 @@ module.exports = function (app, options) { // __(key, value) return util.format(text, value); } - if (arguments.length === 3) { - // __(key, value1, value2) - return util.format(text, value, value2); - } - if (arguments.length === 4) { - // __(key, value1, value2, value3) - return util.format(text, value, value2, value3); - } - if (arguments.length === 5) { - // __(key, value1, value2, value3, value4) - return util.format(text, value, value2, value3, value4); - } - // __(key, value1, value2, value3, value4, value5, ...) + // __(key, value1, ...) var args = new Array(arguments.length); args[0] = text; for(var i = 1; i < args.length; i++) {