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?

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.

If Douglas Crockford, who knows more about Javascript then anyone…say it is so then that probably means it is true?

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:

document.getElementsByTagName('table')[0].tBodies[0].rows[0]

rather than

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.