cache reverse lookups from player name to guid - this prevents the potentially slow iteration throughout localStorage for every lookup

This commit is contained in:
Jon Atkins 2013-05-19 03:50:47 +01:00
parent f1a6909c8e
commit 9a30c13413

View File

@ -17,14 +17,21 @@ window.getPlayerName = function(guid) {
return '{'+guid.slice(0, 12)+'}';
}
window._playerNameToGuidCache = {};
window.playerNameToGuid = function(playerName) {
var cachedGuid = window._playerNameToGuidCache[playerName];
if (cachedGuid !== undefined) return cachedGuid;
var guid = null;
$.each(Object.keys(localStorage), function(ind,key) {
if(playerName === localStorage[key]) {
guid = key;
return false;
return false; //break from $.each
}
});
window._playerNameToGuidCache[playerName] = guid;
return guid;
}