SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Location
    California
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code optimization not necessary?

    I just watched a short presentation by Douglas Crockford where he said that the benefits of optimizing JS were insignificant. His reasoning was that the DOM is so inefficient and spends so much time rendering layout, style, etc, that it negates the benefits of optimizing code. Is this true?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mattSanchez View Post
    I just watched a short presentation by Douglas Crockford where he said that the benefits of optimizing JS were insignificant. His reasoning was that the DOM is so inefficient and spends so much time rendering layout, style, etc, that it negates the benefits of optimizing code. Is this true?
    It is true. When 5% of the rendering time for the page is the JavaScript running, and 30% of the time is DOM access things, even infinitely fast JavaScript won't achieve as much benefit as when compared with achieving a 20% improvement in the DOM performance.

    Have you heard the phrase about premature optimization being the root of all evil? Before optimizing, figure out where the slowdowns and bottlenecks are, so that your optimizations can then be effectively targeted at dealing with the actual problem that is being faced.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If Douglas Crockford, who knows more about Javascript then anyone...say it is so then that probably means it is true?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Just because it makes no significant difference to run times does not mean that there is no point in applying optimisations to javaScript.

    Where the optimised version is just as quick to write and just as easy to read as the unoptimised version then using the optimised version still makes sense.

    Where the optimised version results in less typing and easier to read code then there is even less reason for using the alternative.

    Often using a slightly better optimised version means that you are using code that is easier to read and maintain.

    There are of course also a lot of optimisations that would make the code run slightly faster but which make the code far harder to read and understand - these should always be avoided as readability is far more significant than speed with JavaScript.


    So for example I'd reference the first data row in a table using:

    Code:
    document.getElementsByTagName('table')[0].tBodies[0].rows[0]
    rather than

    Code:
    document.getElementsByTagName('table')[0].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[0]
    because it is shorter to type and easier to read and the fact that the shorter version is actually a more efficient way to do the access iin most modern browsers is an added benefit rather than a reason.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •