50 JavaScript Tips PDF
50 JavaScript Tips PDF
50 JavaScript Tips PDF
INTRODUCTION
This tip-list is for developers who are familiar with JavaSctipt basics and want to learn how to get more out of
JavaScript. Squeezing the most performance out of JavaScript is critical for businesses that rely on their
applications’ performance and uptime.
In the list we have compiled not only the most essential JavaScript performance tips but also the freshest ones
that will enhance web-based mobile applications. The list includes tips from Google, Nokia, Opera, Monitis and
several developers who work really hard to achieve the best possible performance of JavaScript.
A Monitis Publication
Monitis is a specialist provider of web and Cloud monitoring services that include website monitoring, site load
testing, transaction monitoring, application and database monitoring, Cloud resource monitoring, and server and
internal network monitoring within one easy-to-use dashboard. Over 100,000 users worldwide have chosen
Monitis as their provider of choice to increase uptime and user experience of their services and products. What
makes Monitis' solutions different is that they are fast to deploy, feature-rich in technology and provide a
comprehensive single-pane view of on-premise and off-premise infrastructure and applications.
Keep in touch
Publication of
2
Publication of
3
Publication of
4
Publication of
5
Publication of
6
performance impact of destroying and recreating the scripting environment in between page loads. Its is
important to ensure that XMLHttpRequest is supported, or otherwise it can lead to problems and confusion.
When web pages have high amounts of varying content, it can cause pages significant lag time.
Components including images, style sheets and scripts often cause the loading process to slow down.
You can make your pages load faster by reducing the size of your images or changing the format from high-
resolution JPEG files to GIF or PNG format.
You can also clean your code, ridding it of unnecessary characters, including tabs, spaces and comments.
You can use one of the many free tools, like Clean CSS that reduces your code.
Image maps can cause confusion for navigation to users of your webpage, and you want super-easy
navigation. Image maps are good for displaying information in image format, but only with grouped images.
Another downside of using image maps is that they are difficult to create and time-consuming. There is a
large amount of code — which can cause slower download and coordinates must have precision. Even
when you can see the image map, it can look confusing.
New alternatives to image maps include programs such as CSS Sprites or using in-line images.
A web page that hosts many images eats up computer resources and can cause crashes. Keep crashes
minimal by streamlining your code; keeping it simple and clean, host photos and other high-resolution
images on independent sites. A better option is to use CSS Sprites, which allows you to combine an
unlimited number of images while lowering the amount of HTTP requests for a single image, which speeds
the load time of retrieving images from one source instead of combining from separate locations.
Publication of
7
JavaScript is a powerful tool used by many developers, but there are times that using it can slow down
pages, or worse, a browser could disable it and make the page look off to users.
Make sure you absolutely need to use JavaScript, because it may have compatibility issues. If you can
create a better result by using HTML and CSS, by all means use them, and only use JavaScript if the other
way does not work.
Just like asking ten questions when one would suffice is the same principle of HTTP requests. When you
receive all your information with one request, it makes load time faster.
For example, each HTTP request uses 500 bytes – and each received object uses 500 bytes plus
headers. Each additional request sent uses more bytes instead of one 500 byte request and
reply, you use thousands of bytes for the same-sized image.
Traditional imaging methods use spliced portions, which rejoins the pieces for end users. Each slice requires
a separate request; where, using image maps, inline images or CSS Sprites reduces HTTP requests and
speeds up the process by sending images in a single piece or fewer pieces.
It is important to rework one area of your website at a time, so you can check what changes help load times,
what elements you can work without and finding other time-consuming culprits, including server
performance issues.
Some programs that can help in the server-side process include HTTPWatch and Fiddler. Cuzillion helps
when you want to experiment with various configurations of your web page.
You can reduce your HTTP requests by using third-party services. Allowing a third-party to host your images
and RSS feeds allows for one HTTP request and creates faster load times.
Some top third-party hosts allow users to look at your images in different sizes, according to their
preferences.
Flickr provides hotlinks you can embed on your web page, but you cannot directly link to each particular
size.
ImageShack offers free services, but you have to allow ads to show to enjoy that freedom.
Publication of
8
Mobypicture hosts your photos, allows you to keep all rights to your content and does not sell anything
Twitter has joined the image hosting game, but works with PhotoBucket so all the same rules apply.
When choosing third-party hosting make sure to read and heed all rules and regulations. If you worry about
copyrights and the company selling your content, find hosting that allows you to keep all rights to your
content and says so.
Large image files have the potential to overload servers and make web pages load slowly.
You can cut image sizes by using online services that make them smaller. Many online services allow you to
resize your images on their site without having to sign up for use. A couple of sites you can try to get started
include Shrinkpictures.com and JPEGreducer.com.
Another effective way to make images load faster is to change their format. You can use Paint or Photoshop
to convert files from JPEG photo-quality images to lower resolution GIFs.
When changing images from JPEG to GIF you lose high-definition quality, so using a GIF works well for
charts and 2-D images; whereas, JPEG preserves quality when needed.
The best websites offer quality content, quickly. It is important for you to analyze your site to make sure
download speeds stay fast. If you offer great information, but users have to wait, they often leave before the
page loads. When you depend on revenue from your web pages, users leaving early make your money
adventures slim.
Testing your web page loading time is usually a simple process. Monitor.Us provides a free enterprise-
grade page load monitor for its users. The monitor is testing not only JavaScript performance, but also a set
of other important metrics such as the load speed of different elements on your website, how internal and
external links are affected during loading etc
.
Remember that jumbled, extensive or incorrect code causes your pages to load slower or not at all. Using a
source code cleaner, helps you navigate your code, cut unnecessary information and find and correct errors.
HTML Optimizer at iWebTools site allows you to copy and paste your code, and it automatically optimizes
your code. It may not look as good as before you inserted it, but it can make load times faster.
HTML Tidy checks and cleans source files and works well within deeply nested HTML. It also works on hard-
to-read code, making it more comprehensive with user-selected options including indentation and word
wrap.
Publication of
9
Word 2000 HTML Cleaner works well for programmers who write code in MS Word 2000. For $79, you
receive source code ready for use in ASP application. Obviously, this code cleaner targets a specific
audience, but may help you save time with complex codes.
123 HTML Protector Software protects your web page code and prevents others from using your source
code, scripts, links and images. With this program, it makes it difficult for users to manipulate your code,
which could wreak havoc on your pages.
Publication of
10
(Number.prototype.toFixed)?JS_ver.push("1.5"):false;
("".trimLeft)?JS_ver.push("1.8.1"):false;
JS_ver.supports = function()
if (arguments[0])
else
return (this[this.length-1]);
Publication of
11
function createList() {
while (aLI.length) {
oLI.appendChild(document.createTextNode(aLI.shift()));
oFrag.appendChild(oLI);
document.getElementById('myUL').appendChild(oFrag);
Publication of
12
47. DON'T INCLUDE A QUERY STRING IN THE URL FOR STATIC RESOURCES.
Most proxies, most notably Squid up through version 3.0, do not cache resources with a "?" in their URL
even if a Cache-control: public header is present in the response. To enable proxy caching for these
resources, remove query strings from references to static resources, and instead encode the parameters
into the file names themselves.
48. DON'T ENABLE PROXY CACHING FOR RESOURCES THAT SET COOKIES.
Setting the header to public effectively shares resources among multiple users, which means that any
cookies set for those resources are shared as well. While many proxies won't actually cache any resources
with cookie headers set, it's better to avoid the risk altogether. Either set the Cache-Control header to private
or serve these resources from a cookieless domain.
49. LOOP
When simple iterations are required, in other words repeating an action n times, we will often use a while
instead of a for loop.
// do your stuff
vs
Publication of
13
var i = 1000;
while (i--) {
// do your stuff
Anyone who has to deal with (read: support) Internet Explorers will say that the while loop is faster. That is
true for all IE9 and older. New browsers, however, cancel this out in favour of the for loop. Yet, the
performance increase in percentage is higher than the one found in IEs.
console.log(App.view.tablet.Viewport[i]);
Let’s dissect the line inside the for loop. I am telling browser to access App object, then find view reference
and access it, then find tablet and open it to access Viewport. That’s four references so far plus the last one
of the value of i. To reach each reference our JavaScript code communicates with the browser, the browser
communicates through its internal components to the OS and finally to reach the desired memory allocation
in RAM. And we do this five times.
Instead, let’s cache the static part, or the first four steps and see what it brings us:
var vp = Ext.view.tablet.Viewport;
console.log(vp[i]);
Publication of
14
SUMMARY
As mobile devices become more popular among users, it is important to optimize applications for mobile
usage. The need for continually optimizing performance in JavaScript is obvious but as we take into
consideration the complexities of this in regards to mobile applications it becomes imperative.. This tip list is
compiled with mobile performance in mind, so that developers could push their applications’speed and
overall performance even further.
Publication of