Inye 55c055425b Avoid using Object.keys(obj).length in hot code paths
Complexity of Object.keys(obj).length is O(n) where n is the number of
object keys. Unfortunately, JavaScript have no built-in means of
determining number of object keys in constant time. Therefore, we have
to count object keys using separate counter variable.

It may look ugly (and indeed it is), but it greatly improves smoothness
and overall feel of IITC.
2013-08-02 20:00:03 +04:00

127 lines
3.6 KiB
JavaScript

// DEBUGGING TOOLS ///////////////////////////////////////////////////
// meant to be used from browser debugger tools and the like.
window.debug = function() {}
window.debug.renderDetails = function() {
console.log('portals: ' + window.portalsCount);
console.log('links: ' + window.linksCount);
console.log('fields: ' + window.fieldsCount);
}
window.debug.printStackTrace = function() {
var e = new Error('dummy');
console.log(e.stack);
return e.stack;
}
window.debug.clearPortals = function() {
for(var i = 0; i < portalsLayers.length; i++)
portalsLayers[i].clearLayers();
}
window.debug.clearLinks = function() {
linksLayer.clearLayers();
}
window.debug.clearFields = function() {
fieldsLayer.clearLayers();
}
window.debug.getFields = function() {
return fields;
}
window.debug.forceSync = function() {
localStorage.clear();
window.playersToResolve = [];
window.playersInResolving = [];
debug.clearFields();
debug.clearLinks();
debug.clearPortals();
updateGameScore();
requestData();
}
window.debug.console = function() {
$('#debugconsole').text();
}
window.debug.console.show = function() {
if (window.isSmartphone()) {
$('#scrollwrapper, #updatestatus').hide();
$('#map').css('visibility', 'hidden');
}
$('#chat, #chatinput').show();
window.debug.console.create();
$('#chatinput mark').css('cssText', 'color: #bbb !important').text('debug:');
$('#chat > div').hide();
$('#debugconsole').show();
$('#chatcontrols .active').removeClass('active');
$("#chatcontrols a:contains('debug')").addClass('active');
}
window.debug.console.create = function() {
if($('#debugconsole').length) return;
$('#chatcontrols').append('<a>debug</a>');
$('#chatcontrols a:last').click(window.debug.console.show);
$('#chat').append('<div style="display: none" id="debugconsole"><table></table></div>');
}
window.debug.console.renderLine = function(text, errorType) {
debug.console.create();
switch(errorType) {
case 'error': var color = '#FF424D'; break;
case 'warning': var color = '#FFDE42'; break;
default: var color = '#eee';
}
if(typeof text !== 'string' && typeof text !== 'number') {
var cache = [];
text = JSON.stringify(text, function(key, value) {
if(typeof value === 'object' && value !== null) {
if(cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null;
}
var d = new Date();
var ta = d.toLocaleTimeString(); // print line instead maybe?
var tb = d.toLocaleString();
var t = '<time title="'+tb+'" data-timestamp="'+d.getTime()+'">'+ta+'</time>';
var s = 'style="color:'+color+'"';
var l = '<tr><td>'+t+'</td><td><mark '+s+'>'+errorType+'</mark></td><td>'+text+'</td></tr>';
$('#debugconsole table').prepend(l);
}
window.debug.console.log = function(text) {
debug.console.renderLine(text, 'notice');
}
window.debug.console.warn = function(text) {
debug.console.renderLine(text, 'warning');
}
window.debug.console.error = function(text) {
debug.console.renderLine(text, 'error');
}
window.debug.console.overwriteNative = function() {
window.debug.console.create();
window.console = function() {}
window.console.log = window.debug.console.log;
window.console.warn = window.debug.console.warn;
window.console.error = window.debug.console.error;
}
window.debug.console.overwriteNativeIfRequired = function() {
if(!window.console || L.Browser.mobile)
window.debug.console.overwriteNative();
}