How to reduce jQuery?

I use only a few basic jQuery functions on my site.
How to remove unused jQuery code? Knowing only the basics, I looked at the code and did not know where to start. If you could point me in the right direction, that would be appreciated.

Thanks.

+4
source share
5 answers

jQuery itself is not built for custom use. There are many interdependencies between its own functions, and the source is not laid out to be easily modular. Unlike the situation with plugins such as jQuery-UI, the jQuery core is pretty much β€œit-or-leave-it”.

If you used only a few simple functions, you can rewrite them in simple JavaScript. If the main function that you use is a selector, you can use Sizzle , which is the basic selector library that jQuery uses. Otherwise ... really.

+13
source

If you want to reduce the size of the code even further, reduce gzip or deflate it. Beware that the gzipped code must be uncompressed before it is executed, which causes a delay. The jQuery download page offers an already modified version with gzipped support.

Another option you can explore is to use jQuery hosted on CND, i.e. jQuery hosted by third parties such as Google or Microsoft. Details on the jQuery download page .

+5
source

The question is why? The production version of jQuery 1.4.2 is only 24Kb. With proper management of the file cache, it will be downloaded only once and cached. Why bother?

+4
source

As already mentioned, you do not need to do this.

If you are concerned about bandwidth, you can jQuery link from the Google AJAX API , for example:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script> 

To answer the question, it would be extremely difficult.

As you noticed, jQuery is written very red and efficiently and does not have easily deleted fragments.

+2
source

In addition to minimizing / compressing / CDN hosting your Javascript, which you should do for any Javascript library, check out Zepto , whose goal is a more subtle jQuery alternative designed for a mobile browser. This is not a replacement for jQuery, but it supports all the important things like selector, AJAX operations and utilities. I wrote a short blog post on the topic: http://blog.straylightrun.net/2012/10/23/so-you-want-to-use-jquery-in-your-javascriptwidget/

0
source

Source: https://habr.com/ru/post/1308301/


All Articles