Web Optimizations: Java Script and DOM
Web Optimizations: Java Script and DOM
• <ul id='ourlist'>
• …….
• </ul>
Memory Management
• With the rise of JavaScript-heavy web applications, things have
changed.
• It’s quite feasible that the user may spend his or her entire session just
on one page, with Ajax being used to communicate with the server.
• As a result, inefficient memory usage has the opportunity to mount up
into something more significant.
• JavaScript uses the garbage collection methodology for managing
memory.
• Memory is allocated to objects and reclaimed by the garbage-
collection process when the object is no longer being used.
• Let’s look at an example using one of the most common sources of
such circular references:
Language Constructs
• it’s generally DOM manipulation that is slow, rather than JavaScript itself.
• Nevertheless, there are situations in which JavaScript can perform poorly.
• Let’s look at the common cases.
• Loops
for (var x=0; x < myarray.length; x++) {
...}
• You can improve the situation dramatically by fetching the size of the
array outside of the loop,
var count = myarray.length;
for (var x=0; x < count; x++) {
...}
var count = myarray.length;
for (var x=count; x--;) {
...}
• The reason for the poor performance of eval() is that browsers typically
did not cache the compiled code.
• shows a waterfall after merging the five scripts into a single file (combined.js).
As you can see, you save about 0.4 second by merging.
• The relevance of this test has also suffered from short-sightedness. It fails to
consider how the overall page loading time is affected. If you add four images
to the page, the waterfall view (again with IE8) looks as shown in figure
• In the following figure, the combined JavaScript file is used. Resource 3 is
fetched over the existing connection, but resources 4 and 5 each require a new
connection to be opened.
• The following figure shows the defer attribute is used to load the
combined JavaScript file.
• No blocking occurs, and three of the images are requested
immediately.
• the defer attribute is used on each of the four JavaScript resources.
• This leads to an increase in the amount of parallelization, but the effect is still
an increase in loading time.
• The combined version is approximately 1 second faster to complete.