Optimising your JavaScript

0826c9e6449c9a08230ac6d40a3cde4d?s=47 Wendy Liu
February 26, 2014

Optimising your JavaScript

0826c9e6449c9a08230ac6d40a3cde4d?s=128

Wendy Liu

February 26, 2014
Tweet

Transcript

  1. 6.
  2. 14.

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

    for (...) { element = document.getElementById(...); ... }
  3. 15.

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

    setTimeout('someFunction', 1000); setTimeout(someFunction, 1000)
  4. 17.

    speeding up code Cache out of scope variables Cache array.length

    String-building Cache calculations someFunction = someObject.someFunction; for (...) { someFunction(...); }
  5. 18.

    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++) { ... }
  6. 19.

    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('');
  7. 21.

    working with the dom Use CSS Keep it small (virtual

    rendering) Minimise reflows/repaints position: absolute; left: 50%; margin-left: -200px; width: 400px;
  8. 26.

    using prototypes properly Functions Value-type instance variables var Person =

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