0

I use a library which provides lots and lots of utility functions. It's based on jQuery, so I have to include jQuery too. Both of these libraries are huge in size but I only use a single functionality in a single function only once.

I've gathered all the code in a single .js file and measured code coverage using jscoverage. It seems that only 13% of the statements are executed. This means more than 200KB's of dead code.

Here is what the combined.js file looks like:

// jQuery code copy-pasted here

... 

// libX code copy-pasted here

...

libX().doSomething();

So my questions are:

1) How can I remove all dead code from this one .js file? I've tried Google Closure but then the code doesn't work. I guess it messed up with all the jQuery stuff.

2) I will be serving this .js file composed of jQuery, another library and my own little code to 3rd parties so I don't want jQuery involved, even with noconflict mode. How can I tailor this to my own needs? Perhaps replace the dollar sign with an arbitrary symbol or something?

Any help appreciated.

Murat Derya Özen
  • 2,154
  • 8
  • 31
  • 44
  • jQuery **is** JavaScript so if Google Closure failed somehow, it had nothing to do with jQuery. – Sparky Jan 31 '12 at 17:44
  • Well Google Closure wouldn't even work or accept as input if jQuery was not Javascript but something else... Not saying it's jQuery's fault, but just that Closure compiler didn't work for me when trying to remove dead code... – Murat Derya Özen Jan 31 '12 at 17:52
  • My only point was that there's nothing special or proprietary within the jQuery file... it's just written in pure JavaScript. – Sparky Jan 31 '12 at 19:06

1 Answers1

1

jquery minified is small(<20kb), you can use a cdn so it won't have to be downloaded (and thus won't effect performance) and you'll probably use the rest of jquery's functionality at a later date.

I don't understand this : I will be serving this .js file composed of jQuery, ... so I don't want jQuery involved

And noconfilct does replace the dollar sign with an arbitrary symbol of your choosing, thats what you want isn't it ?

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • jquery-min might be small but the other library isn't. Furthermore; only 3-4 kb's of the 20kb is being used. So why should I serve 16+ kb's extra for each and every usage of my js file? It's expected to be requested 10k+ times a day. As for the noconflict mode, if some 3rd party has some other library on his page (YUI or Prototype or whatever), and decides to use my js file as well, it might mess up with things. – Murat Derya Özen Jan 31 '12 at 17:33
  • 2
    why ? becuase its probably cached in client pc if you use a cdn so it won't make a difference to performance, and you'll find a use for the rest of jquery a later date. The third party library strip out the dead code, minifiy it yourself, is it hosted on a cdn elsewhere. I don't understand what you mean regarding noconflict mode. – NimChimpsky Jan 31 '12 at 17:36
  • Thanks Nim but the only reason I have jQuery included is because libX is dependent on a small portion of it. I appreciate your help but please do not make any assumptions for my specific situation and contribute an answer for my questions only, rather than questioning them. – Murat Derya Özen Jan 31 '12 at 17:42
  • 1
    @Murat: Nim seems to be trying to actually understand your needs, yet you're harassing him for the effort. – Chris Farmer Jan 31 '12 at 17:56
  • @murat: your scolding attitude towards Nim is very unwelcome. Anyone in this community is free to question any OP in order to better understand and solve problems. We're not here to serve your whims. – Sparky Jan 31 '12 at 19:01
  • I'm sorry if my attitude seemed scolding. I already thanked Nim, and I appreciate his effort in trying to help me and improve the community. – Murat Derya Özen Jan 31 '12 at 19:20
  • However, though, my question is still unanswered. To restate it for the purpose of clarity; I use a library called libX, which prompts me to include jQuery. libX is huge but uses only a small portion of jQuery. And, I use only a single function from libX. I am looking for a way to: 1) reduce the size of these two libraries so as to include functionality only which I need/use, 2) make sure jQuery doesn't mess up with someone else's code, if I distribute a code that contains jQuery+libX+my_code_that_calls_a_libX_function – Murat Derya Özen Jan 31 '12 at 19:21