Skip to content

Commit 365c245

Browse files
apapirovskiBridgeAR
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 83a5eef commit 365c245

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

lib/util.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,51 +111,35 @@ function format(f) {
111111
++i;
112112
continue;
113113
}
114+
if (lastPos < i)
115+
str += f.slice(lastPos, i);
114116
switch (f.charCodeAt(i + 1)) {
115117
case 100: // 'd'
116-
if (lastPos < i)
117-
str += f.slice(lastPos, i);
118118
str += Number(arguments[a++]);
119119
break;
120120
case 105: // 'i'
121-
if (lastPos < i)
122-
str += f.slice(lastPos, i);
123121
str += parseInt(arguments[a++]);
124122
break;
125123
case 102: // 'f'
126-
if (lastPos < i)
127-
str += f.slice(lastPos, i);
128124
str += parseFloat(arguments[a++]);
129125
break;
130126
case 106: // 'j'
131-
if (lastPos < i)
132-
str += f.slice(lastPos, i);
133127
str += tryStringify(arguments[a++]);
134128
break;
135129
case 115: // 's'
136-
if (lastPos < i)
137-
str += f.slice(lastPos, i);
138130
str += String(arguments[a++]);
139131
break;
140132
case 79: // 'O'
141-
if (lastPos < i)
142-
str += f.slice(lastPos, i);
143133
str += inspect(arguments[a++]);
144134
break;
145135
case 111: // 'o'
146-
if (lastPos < i)
147-
str += f.slice(lastPos, i);
148136
str += inspect(arguments[a++],
149137
{ showHidden: true, depth: 4, showProxy: true });
150138
break;
151139
case 37: // '%'
152-
if (lastPos < i)
153-
str += f.slice(lastPos, i);
154140
str += '%';
155141
break;
156142
default: // any other character is not a correct placeholder
157-
if (lastPos < i)
158-
str += f.slice(lastPos, i);
159143
str += '%';
160144
lastPos = i = i + 1;
161145
continue;

0 commit comments

Comments
 (0)