user656925
user656925

Reputation:

Pre-processing Emulator for JavaScript ( Timing / Debug Example )

In C++ you can omit compiling debug code by using pre-processing directives in order to keep your compiled code fast and not hindered by debug code not needed in production.

Is there a correlative way to do this in JavaScript? What I have been doing in the past is commenting out debug code, but I want a cleaner way to do it.

An example below shows 4 if statements that activate if debug is set to true. However in production I don't want this checked 4 times when I know it will be set to false. As I mentioned I could cram it into one line and comment it out...but I want a clean way to do it?

/**
 **  cType
 */

function cType( o_p ) {
    if( debug ) {
        var t1, t2, t3, t4, i1, i2, i3; t1 = new Date().getTime();
    }
    o_p = MType[ o_p.model ].pre( o_p ); 
    if ( o_p.result !== 'complete' ) {
        if( debug ) {
            t2 = new Date().getTime();
            console.log( '---------------Send to Server - object_pipe: \n ' + o_p.toSource() ); 
        } 
        var string_pipe = JSON.stringify( o_p );
        cMachine( 'pipe=' + string_pipe , function( string_pipe ) {
            if( debug ) { 
                console.log( '---------------Receive from Server - object_pipe: \n ' + string_pipe ); 
                t3 = new Date().getTime();
            }
            MType[ o_p.model ].post( JSON.parse( string_pipe ) );
            if( debug ) {
                t4 = new Date().getTime(); i1 = t2-t1 ; i2 = t3-t2 ; i3 = t4-t3;
                console.log( '---------------Pre, Transit, Post = ', i1, i2, i3 );  
            }
        } );
    }
}

Upvotes: 1

Views: 123

Answers (3)

user1440875
user1440875

Reputation:

No, Javascript is not compiled, it's interpreted. Therefore it's impossible to have preprocessing directives unless you pass non-standard Javascript - it probably won't be Javascript anymore - code through another utility.

Upvotes: 0

Ateş Göral
Ateş Göral

Reputation: 140050

If you use RequireJS, you can use build pragmas and even the has.js integration to disable/enable code fragments at optimization (minification) time.

Upvotes: 1

Daniel
Daniel

Reputation: 31579

You can always pass it through c preprocessor like:

gcc -E input.js -o output.js

This will allow you to use #if and even include and macros.

Upvotes: 2

Related Questions