From 365c24591cb0268b13818fe077c63f55de5c8404 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 30 Aug 2017 14:21:08 -0400 Subject: [PATCH] util: remove duplicate code in format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/15098 Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Evan Lucas Reviewed-By: Colin Ihrig --- lib/util.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/lib/util.js b/lib/util.js index 14a3ce0ead0550..61b7a9d87ea268 100644 --- a/lib/util.js +++ b/lib/util.js @@ -111,51 +111,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;