1. Minimise Access to Page Elements

 

var wrapper = document.getElementById("wrapper");

      header = wrapper.getElementsByTagName("header")[0]

     nav = wrapper.getElementsByTagName("nav")[0]

 

header.className += " " + nav.className;

 

2. To improve performance, duplicate existing elements rather than creating new ones from scratch.

3. Use CSS to Manipulate Page Styles Rather Than JavaScript

   3.1 Reducing browser reflows by hiding  elements whilst altering their style properties. 

     var    nav = wrapper.getElementsByTagName("nav");

      nav.style.display = "none";

4. Avoid Creating Function Within Loops

5. Basic Performance Measurements  function

   var startTime, 

          endTime,

         duration;

function doSomething(){

         var index = 0,

                  length = 10000000,

                  counter = 0 ;

        for(; index < length ; index ++){

                       counter += index;

        }

}

startTime = new Date();

doSomething();

endTime = new Date();

duration = endTime.getTime() - startTime.getTime();

alert(duration);

 

 

4. Geolocation sensor.

 

The W3C Geolocation API

navigation.geoloction.getCurrentPosition()

navigation.geolocation.watchPosition()

 

5. Touch Sensor

    Developing for Multi-Touch Web Browsers

6, Motion Sensor

    Device Motion Event Class Reference

7. Detecting a change in network connection at any point in a Javascript application

 

function goneOffline() {

     alert('No network connection');

}

 

function backOnline(){

alert('The network connection has been restored');

}

window.addEventListener("offline", goneOffline, false);

window.addEventListener("online", backOnline, false);

 

Locating Memory Leaks

First, memory leaks cna occur through the use of the console object to log the value of an object to the Javascript console in the browser developer tools

 

Second, references to Javascript function closure are another common source of memory leaks in the web applications. Imagine an event handler closures are another common source of memory though the rest of the code base may no longer required or used after a certain point in your code, the fact that the closure could potentially be executed, and thus reference that object, means that the object will be kept in memory for as long as that event handler is still alive. 

 

Finally, memory leaks can occur due to stored references between two or more objects that keep the memory allocated by one of those objects reserved despite it no  longer being needed by the application. 

 

 

Locating Memory Leaks

First, memory leaks cna occur through the use of the console object to log the value of an object to the Javascript console in the browser developer tools

 

Second, references to Javascript function closure are another common source of memory leaks in the web applications. Imagine an event handler closures are another common source of memory though the rest of the code base may no longer required or used after a certain point in your code, the fact that the closure could potentially be executed, and thus reference that object, means that the object will be kept in memory for as long as that event handler is still alive. 

 

Finally, memory leaks can occur due to stored references between two or more objects that keep the memory allocated by one of those objects reserved despite it no  longer being needed by the application. 

 

 

 

 

 

arrow
arrow
    全站熱搜

    Kenneth 發表在 痞客邦 留言(0) 人氣()