From 5e9a018b23a324b43aa3376f428b05cd4be06859 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 21 Jul 2015 01:45:11 +0800 Subject: [PATCH] revert slice arguments to args - revert 4ecffe8d22368f4a2cb65113600d9a514b10274e - also add benchmark --- benchmark/arguments-to-args.js | 61 ++++++++++++++++++++++++++++++++++ index.js | 5 ++- package.json | 6 ++-- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 benchmark/arguments-to-args.js diff --git a/benchmark/arguments-to-args.js b/benchmark/arguments-to-args.js new file mode 100644 index 0000000..463ad13 --- /dev/null +++ b/benchmark/arguments-to-args.js @@ -0,0 +1,61 @@ +var Benchmark = require('benchmark'); +var benchmarks = require('beautify-benchmark'); + +var suite = new Benchmark.Suite(); + +function slice() { + return Array.prototype.slice.call(arguments); +} + +function slice0() { + return Array.prototype.slice.call(arguments, 0); +} + +function forLoop() { + var args = new Array(arguments.length); + for(var i = 0; i < args.length; i++) { + args[i] = arguments[i]; + } + return args; +} + +console.log('slice(0, 1, 2, 3, 4, 5, 6, 7): %j', slice(0, 1, 2, 3, 4, 5, 6, 7)); +console.log('slice0(0, 1, 2, 3, 4, 5, 6, 7): %j', slice0(0, 1, 2, 3, 4, 5, 6, 7)); +console.log('forLoop(0, 1, 2, 3, 4, 5, 6, 7): %j', forLoop(0, 1, 2, 3, 4, 5, 6, 7)); + +suite + +.add('Array.prototype.slice.call(arguments)', function() { + slice(0, 1, 2, 3, 4, 5, 6, 7) +}) +.add('Array.prototype.slice.call(arguments, 0)', function() { + slice0(0, 1, 2, 3, 4, 5, 6, 7) +}) +.add('for(var i = 0; i < args.length; i++) {}', function() { + forLoop(0, 1, 2, 3, 4, 5, 6, 7) +}) + +.on('cycle', function(event) { + benchmarks.add(event.target); +}) +.on('start', function(event) { + console.log('\n arguments to args Benchmark\n node version: %s, date: %s\n Starting...', + process.version, Date()); +}) +.on('complete', function done() { + benchmarks.log(); +}) +.run({ async: false }); + +// slice(0, 1, 2, 3, 4, 5, 6, 7): [0,1,2,3,4,5,6,7] +// slice0(0, 1, 2, 3, 4, 5, 6, 7): [0,1,2,3,4,5,6,7] +// forLoop(0, 1, 2, 3, 4, 5, 6, 7): [0,1,2,3,4,5,6,7] +// +// arguments to args Benchmark +// node version: v2.4.0, date: Tue Jul 21 2015 01:39:54 GMT+0800 (CST) +// Starting... +// 3 tests completed. +// +// Array.prototype.slice.call(arguments) x 4,537,649 ops/sec ±1.18% (94 runs sampled) +// Array.prototype.slice.call(arguments, 0) x 4,605,132 ops/sec ±0.87% (96 runs sampled) +// for(var i = 0; i < args.length; i++) {} x 30,435,436 ops/sec ±0.91% (93 runs sampled) diff --git a/index.js b/index.js index 7d5a7df..d9d9eeb 100644 --- a/index.js +++ b/index.js @@ -133,8 +133,11 @@ module.exports = function (app, options) { } // __(key, value1, value2, value3, value4, value5, ...) - var args = Array.prototype.slice.call(arguments); + var args = new Array(arguments.length); args[0] = text; + for(var i = 1; i < args.length; i++) { + args[i] = arguments[i]; + } return util.format.apply(util, args); }; diff --git a/package.json b/package.json index ff3179a..8c409a6 100644 --- a/package.json +++ b/package.json @@ -18,14 +18,16 @@ "dependencies": { "debug": "~2.2.0", "humanize-ms": "~1.0.1", - "ini": "~1.3.3" + "ini": "~1.3.4" }, "devDependencies": { "autod": "*", + "beautify-benchmark": "~0.2.4", + "benchmark": "~1.0.0", "contributors": "*", "istanbul-harmony": "*", "jshint": "*", - "koa": "~0.20.0", + "koa": "~0.21.0", "mm": "~1.1.0", "mocha": "*", "pedding": "~1.0.0",