Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: improve util.format performance #5360

Merged
merged 3 commits into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions benchmark/util/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const util = require('util');
const common = require('../common');
const v8 = require('v8');
const bench = common.createBenchmark(main, {
n: [1e6]
, type: ['string',
'number',
'object',
'unknown',
'no-replace']
});

const inputs = {
'string': ['Hello, my name is %s', 'fred'],
'number': ['Hi, I was born in %d', 1942],
'object': ['An error occurred %j', {msg: 'This is an error', code: 'ERR'}],
'unknown': ['hello %a', 'test'],
'no-replace': [1, 2]
};

function main(conf) {
const n = conf.n | 0;
const type = conf.type;

const input = inputs[type];

v8.setFlagsFromString('--allow_natives_syntax');

util.format(input[0], input[1]);
eval('%OptimizeFunctionOnNextCall(util.format)');
util.format(input[0], input[1]);

bench.start();
for (var i = 0; i < n; i++) {
util.format(input[0], input[1]);
}
bench.end(n);
}
84 changes: 59 additions & 25 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,73 @@ const isError = internalUtil.isError;

var Debug;

const formatRegExp = /%[sdj%]/g;
function tryStringify(arg) {
try {
return JSON.stringify(arg);
} catch (_) {
return '[Circular]';
}
}

exports.format = function(f) {
if (typeof f !== 'string') {
var objects = [];
const objects = new Array(arguments.length);
for (var index = 0; index < arguments.length; index++) {
objects.push(inspect(arguments[index]));
objects[index] = inspect(arguments[index]);
}
return objects.join(' ');
}

if (arguments.length === 1) return f;

var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j':
try {
return JSON.stringify(args[i++]);
} catch (_) {
return '[Circular]';
}
// falls through
default:
return x;
var argLen = arguments.length;

if (argLen === 1) return f;

var str = '';
var a = 1;
var lastPos = 0;
for (var i = 0; i < f.length;) {
if (f.charCodeAt(i) === 37/*'%'*/ && i + 1 < f.length) {
switch (f.charCodeAt(i + 1)) {
case 100: // 'd'
if (a >= argLen)
break;
if (lastPos < i)
str += f.slice(lastPos, i);
str += Number(arguments[a++]);
lastPos = i = i + 2;
continue;
case 106: // 'j'
if (a >= argLen)
break;
if (lastPos < i)
str += f.slice(lastPos, i);
str += tryStringify(arguments[a++]);
lastPos = i = i + 2;
continue;
case 115: // 's'
if (a >= argLen)
break;
if (lastPos < i)
str += f.slice(lastPos, i);
str += String(arguments[a++]);
lastPos = i = i + 2;
continue;
case 37: // '%'
if (lastPos < i)
str += f.slice(lastPos, i);
str += '%';
lastPos = i = i + 2;
continue;
}
}
});
for (var x = args[i]; i < len; x = args[++i]) {
++i;
}
if (lastPos === 0)
str = f;
else if (lastPos < f.length)
str += f.slice(lastPos);
while (a < argLen) {
const x = arguments[a++];
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
str += ' ' + x;
} else {
Expand Down