(This is a bit of an XY problem, but I decided to ask a question that interests me, not the one that I really need at the moment.) I know that in various modern JavaScript machines there are code breakers and other means to get rid of the code that has no effect or side effect, but how do you define and / or compose such code?
The Wikipedia article on eliminating dead code provides one simple example of unreachable code, that is, code that occurs after an unconditional return in a function. But can I count on modern, basic JavaScript mechanisms to eliminate such code? For example, will Rhino or V8 eliminate this code?
function (foo) { return; return foo; } function (foo) { foo = foo; }
And what are no op functions?
(function () {}(foo)); jQuery.noop(foo);
All of these examples fool JSHint, and while JSLint catches the weird assignment foo = foo , you can easily trick it with noops or a couple of variables:
function (foo) { var bar = foo; }
If they can trick static code analyzers, will they cheat the engines themselves?
Except for a thorough study of the source of all the various JavaScript engines, is there a way to identify and / or construct code that will certainly be removed before the program runs, and should it be considered an error if such code is not canceled or is it just a choice design?
source share