Lately I've been writing very processor heavy Javascript. I feel like it could benefit a lot from having a syntax feature for removing debug statements. Obviously JS is interpreted and not compiled, so I'm not sure if this sounds completely unrealistic, but it has some very useful scenarios.
I like to write verbose type checking for functions to check ranges and throw exceptions if invalid input is detected. The issue is in a production environment (especially with games) the code executes too slowly with all the extra branches. It would be nice if there was a simple syntax to treat code as if it's commented out when a flag is set. In some languages this is done with preprocessor statements like: #if debug console.log("Debug Mode"); #else console.log("Release Mode"); #endif The alternative is simply: const debug = false; if (debug) { // Tons of type checking } What I'd expect would be possible for an implementer is to get to the constant and evaluate the branch and remove the whole statement before running it through the JIT. This would allow a very standard way to turn on and off pieces of code. An example program: // Debug Off Control { console.time("benchmark control"); for (var i = 0; i < 10000000; ++i) { } console.timeEnd("benchmark control"); } // Debug Off { const debugOff = false; var debugOffCounter = 0; console.time("benchmark debug off"); for (var i = 0; i < 10000000; ++i) { if (debugOff) { debugOffCounter++; } } console.timeEnd("benchmark debug off"); } // Debug On { const debugOn = true; var debugOnCounter = 0; console.time("benchmark debug on"); for (var i = 0; i < 10000000; ++i) { if (debugOn) { debugOnCounter++; } } console.timeEnd("benchmark debug on"); } http://jsfiddle.net/9LCra/ On the latest Firefox there's a 11 ms difference between the control and using a constant in the if. In chrome there's a 23 ms difference. Is there anything stopping an implementer from evaluating the constant and making the control and debug off identical in performance? I kind of want this to be like a standard goto technique that's expected to work since I believe right now the alternative is to simply create two files or remove anything that might slow things down. I decided to post here first in case there's a fundamental reason that such a use case would be impossible or if an alternative was in the works that would fit this goal. _______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss