10 Ways to Instantly Increase Your jQuery Performance

jQuery is in constant development and improvement. John and his team are always researching new ways to improve program performances.
As a sidenote, just a few months ago, he released Sizzle, a selector library that's said to improve program performances up to 3 times in Firefox.

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

* Editor's Note: Perhaps, the quicker and easier method is to simply link to the script directly. Rather than hard-coding the specific version of jQuery directly (1.3.2), you should instead use 1, which will automatically reference the most recent version of the library.

Minify

The majority of browsers are not able to process more than one script concurrently 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 single 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 production web servers is gzipped as part of the HTTP protocol). -- From YUI compressor, an excellent tool jQuery officially reccomends to minify scripts.

Native functions are always faster than any helper counterparts.
Whenever you're looping through an object received as JSON, you'd better rewrite your JSON and make it return an array through which you can loop easier.

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

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

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

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

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

As stated in jQuery's documentation,

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

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

Instead of

always go for contextualized selectors in the form:

thus yielding

which runs much faster, because it doesn't have to traverse the entire DOM -- just the #class-container element.

Do not make the mistake or reusing your selectors time and time again. Instead, you should cache it in a variable. That way, the DOM doesn't have to track down your element over and over again.

Never select elements multiple times inside a loop EVER! It'd be a speed-killer!

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

DOM manipulation should be as limited as possible, since insert operations like prepend(), append(), after() are rather time-consuming.

The above example could be quickened using html() and building the list beforehand.

It might appear strange, but this really helps to speed things, especially when dealing with long strings of text that need to be concatenated.

First create an array and fill it with what you have to join together. The join() method will prove much faster than the string 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 array and joining them at the last minute" and "An array as a string buffer is more efficient on all browsers, with the exception of Firefox 2.0.0.14/Windows, than using String.prototype.concat.apply." -- Tom Trenka

Advertisement

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

So, instead of

take the time to write

This isn't a speed up tip, but could end up, in a round about way, being one if you take the time to find your way through cheatsheets and function references.
Save yourself some time and keep a cheat-sheet within an arm's reach.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值