Merge pull request #6 from hotoo/apply
refact(apply): apply is not too slow than direct call. fixed #2master
commit
135d55773a
|
@ -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
|
18
index.js
18
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++) {
|
||||
|
|
Loading…
Reference in New Issue