A faster implementation for formatGuid function.

This commit is contained in:
Matti Kononen 2013-05-20 17:03:27 +03:00
parent 47e9368769
commit 5edb9f87e7
1 changed files with 12 additions and 2 deletions

View File

@ -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);
}