aboutsummaryrefslogtreecommitdiff
path: root/lib/console.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-04-15 13:13:49 -0700
committerJames M Snell <jasnell@gmail.com>2016-04-20 14:45:42 -0700
commit33c242e7e98725e9cca7914186d6ca96eb9b1035 (patch)
treeb32d8f5b58af7a893fad48e2d50a5108f6bf3b14 /lib/console.js
parente7c077c610afa371430180fbd447bfef60ebc5ea (diff)
downloadandroid-node-v8-33c242e7e98725e9cca7914186d6ca96eb9b1035.tar.gz
android-node-v8-33c242e7e98725e9cca7914186d6ca96eb9b1035.tar.bz2
android-node-v8-33c242e7e98725e9cca7914186d6ca96eb9b1035.zip
console: refactor to use rest params and template strings
Overall cleanup in code, eliminate reliance on `arguments`. Benchmarks show that as of v8 5.0.71.32, using rest params + apply has good performance. The spread operator is not yet well optimized in v8 ``` misc/console.js method=restAndSpread concat=1 n=1000000: 374779.38359 misc/console.js method=restAndSpread concat=0 n=1000000: 375988.30434 misc/console.js method=argumentsAndApply concat=1 n=1000000: 682618.61125 misc/console.js method=argumentsAndApply concat=0 n=1000000: 645093.74443 misc/console.js method=restAndApply concat=1 n=1000000: 682931.41217 misc/console.js method=restAndApply concat=0 n=1000000: 664473.09700 ``` PR-URL: https://github.com/nodejs/node/pull/6233 Reviewed-By: Brian White <mscdex@mscdex.net>
Diffstat (limited to 'lib/console.js')
-rw-r--r--lib/console.js31
1 files changed, 15 insertions, 16 deletions
diff --git a/lib/console.js b/lib/console.js
index 78c6955af5..646e268624 100644
--- a/lib/console.js
+++ b/lib/console.js
@@ -35,16 +35,20 @@ function Console(stdout, stderr) {
}
}
-Console.prototype.log = function() {
- this._stdout.write(util.format.apply(null, arguments) + '\n');
+
+// As of v8 5.0.71.32, the combination of rest param, template string
+// and .apply(null, args) benchmarks consistently faster than using
+// the spread operator when calling util.format.
+Console.prototype.log = function(...args) {
+ this._stdout.write(`${util.format.apply(null, args)}\n`);
};
Console.prototype.info = Console.prototype.log;
-Console.prototype.warn = function() {
- this._stderr.write(util.format.apply(null, arguments) + '\n');
+Console.prototype.warn = function(...args) {
+ this._stderr.write(`${util.format.apply(null, args)}\n`);
};
@@ -52,9 +56,8 @@ Console.prototype.error = Console.prototype.warn;
Console.prototype.dir = function(object, options) {
- this._stdout.write(util.inspect(object, util._extend({
- customInspect: false
- }, options)) + '\n');
+ options = Object.assign({customInspect: false}, options);
+ this._stdout.write(`${util.inspect(object, options)}\n`);
};
@@ -66,7 +69,7 @@ Console.prototype.time = function(label) {
Console.prototype.timeEnd = function(label) {
var time = this._times.get(label);
if (!time) {
- throw new Error('No such label: ' + label);
+ throw new Error(`No such label: ${label}`);
}
const duration = process.hrtime(time);
const ms = duration[0] * 1000 + duration[1] / 1e6;
@@ -75,24 +78,20 @@ Console.prototype.timeEnd = function(label) {
};
-Console.prototype.trace = function trace() {
+Console.prototype.trace = function trace(...args) {
// TODO probably can to do this better with V8's debug object once that is
// exposed.
var err = new Error();
err.name = 'Trace';
- err.message = util.format.apply(null, arguments);
+ err.message = util.format.apply(null, args);
Error.captureStackTrace(err, trace);
this.error(err.stack);
};
-Console.prototype.assert = function(expression) {
+Console.prototype.assert = function(expression, ...args) {
if (!expression) {
- const argsLen = arguments.length || 1;
- const arr = new Array(argsLen - 1);
- for (var i = 1; i < argsLen; i++)
- arr[i - 1] = arguments[i];
- require('assert').ok(false, util.format.apply(null, arr));
+ require('assert').ok(false, util.format.apply(null, args));
}
};