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.
No comments:
Post a Comment