From 187cfa1edccaa1c7c7ce600c05028813c970b8f9 Mon Sep 17 00:00:00 2001 From: Jon Apgar Date: Fri, 27 Feb 2015 14:12:13 -0500 Subject: [PATCH] Optimize SqlString.format I was looking for ways to speed up mysql.format after noticing that the performance was very slow when attempting to format a query with 100,000+ values. I came up with a couple of improvements, but by far the most significant is using an iterator to step through the values array instead of Array.shift(). See: http://jsperf.com/some-array-reading-comparisons/6 --- lib/protocol/SqlString.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/protocol/SqlString.js b/lib/protocol/SqlString.js index c9883daa3..86f1ebd4a 100644 --- a/lib/protocol/SqlString.js +++ b/lib/protocol/SqlString.js @@ -66,16 +66,16 @@ SqlString.arrayToList = function(array, timeZone) { SqlString.format = function(sql, values, stringifyObjects, timeZone) { values = values == null ? [] : [].concat(values); - + var index = 0; return sql.replace(/\?\??/g, function(match) { - if (!values.length) { + if (index>=values.length) { return match; } if (match == "??") { - return SqlString.escapeId(values.shift()); + return SqlString.escapeId(values[index++]); } - return SqlString.escape(values.shift(), stringifyObjects, timeZone); + return SqlString.escape(values[index++], stringifyObjects, timeZone); }); };