Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Optimising your JavaScript

Wendy Liu
February 26, 2014

Optimising your JavaScript

Wendy Liu

February 26, 2014
Tweet

More Decks by Wendy Liu

Other Decks in Programming

Transcript

  1. speeding up code Native functions Combine timers Avoid anything eval-like

    for (...) { element = document.getElementById(...); ... }
  2. speeding up code Native functions Combine timers Avoid anything eval-like

    setTimeout('someFunction', 1000); setTimeout(someFunction, 1000)
  3. speeding up code Cache out of scope variables Cache array.length

    String-building Cache calculations someFunction = someObject.someFunction; for (...) { someFunction(...); }
  4. speeding up code Cache out of scope variables Cache array.length

    String-building Cache calculations for (var i = 0, l = arr.length; i < l; i++) { ... }
  5. speeding up code Cache out of scope variables Cache array.length

    String-building Cache calculations var stringBuilder = []; for (...) { stringBuilder.push('a'); } var string = stringBuilder.join('');
  6. working with the dom Use CSS Keep it small (virtual

    rendering) Minimise reflows/repaints position: absolute; left: 50%; margin-left: -200px; width: 400px;
  7. using prototypes properly Functions Value-type instance variables var Person =

    function() {}; Person.prototype.doSomething = function(...) { ... }; var Person = function() { this.doSomething = function(...) { ... }; };