Permalink
Browse files

util: remove duplicate code in format

util.format contains an idential if statement within each branch
of switch. Move it before the switch statement for cleaner code
and better performance.

PR-URL: #15098
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information...
apapirovski authored and MylesBorins committed Aug 30, 2017
1 parent 605d625 commit 2aec977fa26d12119c3672d1ce779d8aadde526b
Showing with 2 additions and 18 deletions.
  1. +2 −18 lib/util.js
@@ -110,51 +110,35 @@ function format(f) {
++i;
continue;
}
if (lastPos < i)
str += f.slice(lastPos, i);
switch (f.charCodeAt(i + 1)) {
case 100: // 'd'
if (lastPos < i)
str += f.slice(lastPos, i);
str += Number(arguments[a++]);
break;
case 105: // 'i'
if (lastPos < i)
str += f.slice(lastPos, i);
str += parseInt(arguments[a++]);
break;
case 102: // 'f'
if (lastPos < i)
str += f.slice(lastPos, i);
str += parseFloat(arguments[a++]);
break;
case 106: // 'j'
if (lastPos < i)
str += f.slice(lastPos, i);
str += tryStringify(arguments[a++]);
break;
case 115: // 's'
if (lastPos < i)
str += f.slice(lastPos, i);
str += String(arguments[a++]);
break;
case 79: // 'O'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++]);
break;
case 111: // 'o'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++],
{ showHidden: true, depth: 4, showProxy: true });
break;
case 37: // '%'
if (lastPos < i)
str += f.slice(lastPos, i);
str += '%';
break;
default: // any other character is not a correct placeholder
if (lastPos < i)
str += f.slice(lastPos, i);
str += '%';
lastPos = i = i + 1;
continue;

0 comments on commit 2aec977

Please sign in to comment.