04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: Variables, Scope, and Memory<br />

There are several other patterns that may cause circular references, which they will<br />

be covered throughout this book.<br />

Performance<br />

The garbage collector runs periodically and can potentially be an expensive process if there is a large<br />

number of variable allocations in memory, so the timing of the garbage - collection process is important.<br />

IE was infamous for its performance issues related to how often the garbage collector ran — it ran based<br />

on the number of allocations, specifically 256 variable allocations, 4,096 object/array literals and array<br />

slots, or 64kb of strings. If any of these thresholds were reached, the garbage collector would run. The<br />

problem with this implementation is that a script with so many variables will probably continue to have<br />

that many variables throughout its lifetime, meaning the garbage collector will run quite frequently. This<br />

issue caused serious performance problems that led to changes in the garbage - collection routine in IE 7.<br />

With the release of IE 7, the JavaScript engine ’ s garbage - collection routine was tuned to dynamically<br />

change the allocation threshold of variables, literals, and/or array slots that triggered garbage collection.<br />

The IE 7 thresholds start out equal to those in IE 6. If the garbage - collection routine reclaims less than<br />

15% of the allocations, the threshold for variables, literals, and/or array slots doubles. If the routine ever<br />

reclaims 85% of the allocations, then the threshold is reset to the default. This simple change greatly<br />

improved the performance of the browser on JavaScript - heavy web pages.<br />

It ’ s possible, though not recommended, to trigger the garbage - collection process<br />

in some browsers. In IE, the window.CollectGarbage() method causes garbage<br />

collection to occur immediately. In Opera 7 and higher, calling window.opera<br />

.collect() initiates the garbage - collection process.<br />

Managing Memory<br />

In a garbage - collected programming environment, developers typically don ’ t have to worry about<br />

memory management. However, JavaScript runs in an environment where memory management and<br />

garbage collection operate uniquely. The amount of memory available for use in web browsers is<br />

typically much less than is available for desktop applications. This is more of a security feature than<br />

anything else, ensuring that a web page running JavaScript can ’ t crash the operating system by using up<br />

all the system memory. The memory limits affect not only variable allocation but also the call stack and<br />

the number of statements that can be executed in a single thread.<br />

Keeping the amount of used memory to a minimum leads to better page performance. The best way to<br />

optimize memory usage is to ensure that you ’ re keeping around only data that is necessary for the<br />

execution of your code. When data is no longer necessary, it ’ s best to set the value to null , freeing up the<br />

reference — this is called dereferencing the value. This advice applies mostly to global values and<br />

93

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!