javascript - Enable/Disable debug code dynamically -


i'm writing decent sized javascript animation library, include debugging code in. check :

if(mylib.debugger){    console.warn('warning message'); } 

however if runs couple thousand times second, cause performance issues. add in few more checks throughout code , effect more noticeable.

what i'm wondering if possible check onload if debugger should enabled, , if so... turn this:

//debugger if(!this.name) console.warn('no name provided'); 

into:

if(!this.name) console.warn('no name provided'); 

leaving code commented if not enabled, , uncommenting if is, removing possible performance issues. done somehow regular expression on entire script if loaded in through ajax? i'm trying avoid need 2 versions of same code, lib.dbug.js , lib.js.

cross browser compatibility not of great importance (i'm worried new browsers), see nice have item. if possible however, great thing have.

any insight appreciated.

the simplest way check if debugger should disabled , if so, replace mock object nothing @ start of script:

if (!mylib.debugger) {     window.console = (function () {         var newconsole = {};         var key;          (key in window.console) {             if (typeof window.console[key] === 'function') {                 newconsole[key] = function () {};             }         }          return newconsole;     }()); } 

the overhead of approach should negligible.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -