Skip to content

Commit 2aec977

Browse files
apapirovskiMylesBorins
authored andcommitted
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 <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 605d625 commit 2aec977

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

lib/util.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -110,51 +110,35 @@ function format(f) {
110110
++i;
111111
continue;
112112
}
113+
if (lastPos < i)
114+
str += f.slice(lastPos, i);
113115
switch (f.charCodeAt(i + 1)) {
114116
case 100: // 'd'
115-
if (lastPos < i)
116-
str += f.slice(lastPos, i);
117117
str += Number(arguments[a++]);
118118
break;
119119
case 105: // 'i'
120-
if (lastPos < i)
121-
str += f.slice(lastPos, i);
122120
str += parseInt(arguments[a++]);
123121
break;
124122
case 102: // 'f'
125-
if (lastPos < i)
126-
str += f.slice(lastPos, i);
127123
str += parseFloat(arguments[a++]);
128124
break;
129125
case 106: // 'j'
130-
if (lastPos < i)
131-
str += f.slice(lastPos, i);
132126
str += tryStringify(arguments[a++]);
133127
break;
134128
case 115: // 's'
135-
if (lastPos < i)
136-
str += f.slice(lastPos, i);
137129
str += String(arguments[a++]);
138130
break;
139131
case 79: // 'O'
140-
if (lastPos < i)
141-
str += f.slice(lastPos, i);
142132
str += inspect(arguments[a++]);
143133
break;
144134
case 111: // 'o'
145-
if (lastPos < i)
146-
str += f.slice(lastPos, i);
147135
str += inspect(arguments[a++],
148136
{ showHidden: true, depth: 4, showProxy: true });
149137
break;
150138
case 37: // '%'
151-
if (lastPos < i)
152-
str += f.slice(lastPos, i);
153139
str += '%';
154140
break;
155141
default: // any other character is not a correct placeholder
156-
if (lastPos < i)
157-
str += f.slice(lastPos, i);
158142
str += '%';
159143
lastPos = i = i + 1;
160144
continue;

0 commit comments

Comments
 (0)