7

I have reports - no source available, I'm afraid - of a web application that doesn't work on devices using some mobile data networks, because the network operators are running a non-transparent proxy that is compressing images and minifying JavaScript, and the minification is actually breaking the code.

I'm curious as to whether anyone has an example (i.e. a code snippet plus a minification technique) whereby sloppy JS code combined with aggressive minification could actually change the meaning of the code? I suspect such a combination is possible but can't think of - or find - any examples. Anyone got a good example, or a proof to the contrary?

Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
  • You don't have a source, but do you have an example of an operator that this happens with? – Nzall Aug 05 '14 at 09:46
  • 1
    Hope you already saw this; may be some bad code like this will get a pass when unminified, but actual error unviels when minified http://stackoverflow.com/questions/16484566/javascript-error-when-minified – sabithpocker Aug 05 '14 at 09:52
  • Rumour has it that it's O2 in the UK - I've not experienced this particular issue first-hand, but I do know from personal experience that O2 use heavy image compression on their mobile data network. – Dylan Beattie Aug 05 '14 at 10:28
  • Please note that minified Javascript and images are actually a good thing on mobile networks. 90% of users have limited bandwidth on their mobile connections, which means those few KB they can save add up over time. – Nzall Aug 05 '14 at 13:18
  • Oh, agreed - minification is good, especially on mobile networks. And JavaScript that won't survive minification is probably bad JavaScript anyway :) – Dylan Beattie Aug 05 '14 at 14:10

1 Answers1

6

consider the following code:

function DoStuff(thingA, ThingB){
    var thingC = thingA + ThingB;
    return thingC;
}

var stuffingC = eval("DoStuff(stuffingA, stuffingB)");

minifiers sometimes shorten variable or function names:

function DS(A, B){return A+B;}

var C= eval("DoStuff(stuffingA, stuffingB)");

In this case, your code would break because the eval'd string isn't changed to account for the changed name of your function.

this is a basic example, but this is often what happens: you have some sort of reflection or evaluation of a string variable that refers to a minified piece of code with the pre-minification name, but isn't changed to account for this minified nature.

Nzall
  • 3,439
  • 5
  • 29
  • 59