From 5edb9f87e7d34fb362a366f0392df0bcbe8dc878 Mon Sep 17 00:00:00 2001 From: Matti Kononen Date: Mon, 20 May 2013 17:03:27 +0300 Subject: [PATCH] A faster implementation for formatGuid function. --- lib/attribute.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/attribute.js b/lib/attribute.js index 0d1668d..32f67c8 100644 --- a/lib/attribute.js +++ b/lib/attribute.js @@ -182,10 +182,20 @@ Attribute.prototype.toString = function () { return JSON.stringify(this.json); }; +// The array contains placeholders for digits in the format. +// Storing search stings in the array makes the search much faster than using regular expressions. +var FORMAT_SEARCH_STRING = []; +function getFormatSearchString(index) { + if (!FORMAT_SEARCH_STRING[index]) { + FORMAT_SEARCH_STRING[index] = '{' + index + '}'; + } + return FORMAT_SEARCH_STRING[index]; +} + function formatGuid(format, data) { for (var i = 0; i < data.length; i++) { - var re = new RegExp('\\{' + i + '\\}', 'g'); - // Leading 0 is needed if value of data[i] is less than 16 (of 10 as hex). + var re = getFormatSearchString(i); + // Leading 0 is needed if value of data[i] is less than 16 (of 10 as hex). var dataStr = data[i].toString(16); format = format.replace(re, data[i] >= 16 ? dataStr : '0' + dataStr); }