Skip to content Skip to sidebar Skip to footer

how to tell what version of jquery is loaded

Read Time: 7 mins Languages:

This article will nowadays ten easy steps that will instantly improve your script's performance. Don't worry; there isn't anything too hard here. Everyone tin can apply these methods! When you're finished reading, please let us know your speed tips.

i. Always Apply the Latest Version

jQuery is in abiding development and improvement. John and his team are always researching new means to improve program performances.
Every bit a sidenote, just a few months agone, he released Sizzle, a selector library that's said to better program performances up to 3 times in Firefox.

If yous want to stay upward to appointment without having to download the library a k times, GIYF (Google Is Your Friend), in this situation too. Google provides a lot of Ajax libraries from which to choose.

* Editor's Annotation: Peradventure, the quicker and easier method is to simply link to the script straight. Rather than difficult-coding the specific version of jQuery directly (1.iii.2), you should instead utilise 1, which will automatically reference the virtually recent version of the library.

ii. Combine and Minify Your Scripts

Minify Minify Minify

The majority of browsers are not able to process more than one script concurrently and so they queue them up -- and load times increase.
Assuming the scripts are to be loaded on every page of your website, you should consider putting them all into a unmarried file and use a compression tool (such as Dean Edwards') to minify them. Smaller file sizes equal faster load times.

The goal of JavaScript and CSS minification is always to preserve the operational qualities of the code while reducing its overall byte footprint (both in raw terms and after gzipping, as most JavaScript and CSS served from product web servers is gzipped equally office of the HTTP protocol). -- From YUI compressor, an excellent tool jQuery officially reccomends to minify scripts.

three. Use For Instead of Each

Native functions are always faster than whatever helper counterparts.
Whenever yous're looping through an object received as JSON, y'all'd better rewrite your JSON and brand it render an assortment through which you can loop easier.

Using Firebug, information technology's possible to measure the time each of the two functions takes to run.

The in a higher place results are 2ms for native code, and 26ms for jQuery'south "each" method. Provided I tested it on my local motorcar and they're not actually doing anything (just a mere assortment filling operation), jQuery's each function takes over 10 times as long as JS native "for" loop. This will certainly increment when dealing with more complicated stuff, similar setting CSS attributes or other DOM manipulation operations.

4. Use IDs Instead of Classes

It'southward much improve to select objects by ID because of the library'south beliefs: jQuery uses the browser'south native method, getElementByID(), to retrieve the object, resulting in a very fast query.

So, instead of using the very handy class selection technique, it'due south worth using a more complex selector (which jQuery certainly doesn't neglect to provide), write your own selector (yes, this is possible, if y'all don't find what you need), or specify a container for the chemical element you need to select.

The above lawmaking really shows the differences between the two ways of selecting elements, highlighting a never-ending over 5 seconds fourth dimension to load the class driven snippet.

5. Requite your Selectors a Context

As stated in jQuery's documentation,

The DOM node context originally passed to jQuery() (if none was passed then context volition be equal to the document).
It should be used in conjunction with the selector to decide the exact query used.

So, if you must utilise classes to target your elements, at to the lowest degree prevent jQuery from traversing the whole DOM using selectors appropriately.

Instead of

always become for contextualized selectors in the course:

thus yielding

which runs much faster, considering information technology doesn't have to traverse the entire DOM -- just the #class-container chemical element.

half dozen. Cache. Ever.

Do non make the mistake or reusing your selectors time and time again. Instead, yous should enshroud information technology in a variable. That way, the DOM doesn't have to runway down your chemical element over and over again.

Never select elements multiple times within a loop EVER! Information technology'd be a speed-killer!

And, as the following chart exemplifies, the results of caching are evident even in relatively short iterations.

7. Avoid DOM Manipulation

DOM manipulation should exist as limited as possible, since insert operations similar prepend(), suspend(), after() are rather time-consuming.

The above case could be quickened using html() and edifice the list beforehand.

viii. No String concat(); Use join() for Longer Strings

It might appear foreign, only this really helps to speed things, peculiarly when dealing with long strings of text that demand to be concatenated.

Showtime create an array and fill it with what you accept to join together. The join() method volition prove much faster than the cord concat() function.

However, recent tests conducted by Tom Trenka contributed to the creation of the following chart.

"The += operator is faster—even more than pushing string fragments into an assortment and joining them at the final infinitesimal" and "An array as a string buffer is more efficient on all browsers, with the exception of Firefox two.0.0.14/Windows, than using String.prototype.concat.utilize." -- Tom Trenka

9. Return Simulated

You may take noticed whenever your functions don't return false, you jump to the peak of the page.
When dealing with longer pages, this consequence can be quite annoying.

So, instead of

accept the time to write

10. Bonus tip - Crook-sheets and Library References

This isn't a speed up tip, just could end up, in a round about way, being 1 if you take the fourth dimension to find your style through cheatsheets and office references.
Salve yourself some time and continue a cheat-sheet within an arm's reach.

  • Follow united states of america on Twitter, or subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.
  • You tin can also find some great jQuery plugins on Envato Market.

Did you find this post useful?

marinderving.blogspot.com

Source: https://code.tutsplus.com/tutorials/10-ways-to-instantly-increase-your-jquery-performance--net-5551

Post a Comment for "how to tell what version of jquery is loaded"