Q:

Enable/Disable debug code dynamically

I'm writing a decent sized JavaScript animation library, that I would like to include debugging code in. I could easily do a check : 
if(myLib.debugger){
   console.warn('warning message');
}

However if this runs a couple thousand times a second, it would eventually cause performance issues. Add in a few more checks throughout the code and the effect will be even more noticeable.
What I'm wondering is if it would be possible to check onload if the debugger should be enabled, and if so... turn something like this:
//debugger if(!this.name) console.warn('No name provided');

into:
if(!this.name) console.warn('No name provided');

Leaving the code commented if its not enabled, and uncommenting it if it is, thus removing any possible performance issues. Could this be done somehow with a regular expression on the entire script if loaded in through ajax? I'm trying to avoid the need for 2 versions of the same code, lib.dbug.js and a lib.js. 
Cross browser compatibility is not of great importance for this (I'm really only worried about new browsers), I see it as nice to have item. If its possible however, it would be a great thing to have. 
Any insight would be greatly appreciated.

A:

The simplest way to do this would be to check if the debugger should be disabled and if so, replace it with a mock object that does nothing at the very start of your script:
if (!myLib.debugger) {
    window.console = (function () {
        var newConsole = {};
        var key;

        for (key in window.console) {
            if (typeof window.console[key] === 'function') {
                newConsole[key] = function () {};
            }
        }

        return newConsole;
    }());
}

The overhead of this approach should be negligible.

