views:

283

answers:

4

Normally I love to keep my HTML code clean, semantic and free from either Javascript or CSS. I include my .JS and .CSS files at the top, and layer functionality on top of the DOM elements.

The positives of this are:

  • Architectural separation of concerns
  • Graceful degradation where Javascript or CSS isn't supported
  • Search-engine friendliness

There is one major negative:

  • Performance problems in IE 6

Because all the events are attached to the elements through Javascript code, which accesses the DOM, performance in IE suffers.

This is especially so when using jQuery (which happens to be my favorite Javascript framework).

So it seems like I have two choices: either keep the code nice and neat, and have IE 6 users (around 20% of the user-base) complain, or "de-normalize" the code to improve IE 6 performance.

Is there a "middle way" in this situation? Or am I doomed?


Note: I'm not saying that my performance problems are caused by having Javascript in a separate file.

I can achieve wonderful performance in IE while keeping it in a separate file.

The problem is, I still have to put the actual event handlers into 'onclick' attributes in the HTML. For example:

<span onclick="doSomething()">More...</span>

It would be much nicer if I could just write:

<span id="more-button">More...</span>

And then assign it separately, in Javascript, with the following:

$("#more-button").click(doSomething);

Unfortunately, it seems this is bad for IE6 performance.

+1  A: 

I don't think your performance problems are because you put your javascript code into separate files. I do that... a lot of people do that... It's considered best practice to do that.

JQuery has some performance issues. That may be one place to look. The other place you need to look is at your javascript code itself. Without seeing that, I wouldn't be able to help you make your application perform better.

Gabriel McAdams
Please see my "note" added above.
jonathanconway
Is the bad performance you mention - on page load when you are attaching event handlers? Or it is when the events are being handled?
Gabriel McAdams
Good question. It's hard to determine. I just installed a product called 'dynaTrace'; hoping it will shed some light.
jonathanconway
+2  A: 

You should actually put your JavaScript at the bottom of the page. As far as performance issues around attaching events, make sure to measure carefully before optimizing. Dynatrace AJAX edition is a great tool for measuring and understanding IE performane.

Annie
A: 

If your code runs on browsers, and just performans bad on IE6, then just feel lucky!

Really, ignore this old beast!

If your code runs perfect on most browsers, and you have to make it slow because IE6 can not run it, then you should display a message to those IE6 users (or just live with bad performance on every browser).

But your situation seems simple and straightforward to me. Do not matter! IE6 is and should be dead!!! Even some big websites have begun to display update notices to IE6 users. Instead of their usual content, they do not support it anymore, because it is crap!

frunsi
Yes, ignore 20% of your users ... -1
Martin
The code isn't going to run slower because of IE performance tuning. That's not the problem. The problem is that IE performance tuning will break separation of concerns and made SEO difficult. So my question is, do I have to choose between either A) clean code, or B) IE performance? Or is some way I can have both?
jonathanconway
@Martin: YES, ignore 20% of your users (and display them a message that their software sucks and that they should udgrade). Yes, yes, yes. Point.@janathanconway: choose A if you can still ensure that it runs on IE. Users that run a year-2002-browser do not matter about performance! IE6 just sucks, and even at 2002 there were better alternatives. At that time MS just won a war, and was living a monopoly dream.
frunsi
Addition: if you optimize for IE6 performance, then good luck. That 20% of users will cost you a major part of your time. And I will not like to answer detailed IE6-related questions, because there are so many bugs in IE6, and I spent so many hours fixing them. I am so happy of everything of the post-IE-6-era. That's the point.
frunsi
Some people work at places that do not allow them to upgrade browsers (like US government jobs). Is it their fault they cannot upgrade, no. Should their experience be penalized, no. Should you show them a message saying their browser is outdated, NO!!!!!! That is tacky.
Martin
@Martin: Alright, choose B.
frunsi
+3  A: 

In a talk available at YUI Theater, Joseph Smarr talks about performance and states the opinion that it is ok to have onmousedown handlers and the like in your code for performance reasons instead of finding the DOM element and then attaching the event handler. However, I do not know any measures by which that speeds up things. Other techniques for unobtrusive performance boosts are (EIDT: note that they are browser independent, hence not restricted to IE):

  • Use onmousedown instead of onclick. Smarr says there is a 100ms or so period between the firing of the two events.
  • For perceived performance: Give instant visual feedback by using window.setTimeout(visualFeedback, 0). That lets the browser draw some graphical change instantly, letting the user know that something is happening.
  • When you have to traverse the DOM, cache intermediate results in a variable to reuse them (guess you know that one).

There is also a demo that shows the benefit of the first two points. The button at the top uses onclick and normal execution, the one below uses onmousedown and setTimeout(func, 0).

Tom Bartel
No, I didn't know about the above boosts. Thanks for telling me. I might consider using these rather than messing up my HTML code.
jonathanconway
I'm not so sure that there is a 100ms delay for onclick. I think the delay is just the fact that the user has to make 2 actions when clicking the mouse instead of 1.
Alex
Ahh so this is isn't an actual technical glitch in IE?
jonathanconway
@Alex: I didn't say that `onclick` has a delay. You are right, of course the `mousedown` event occurs before the `click` event. Maybe my English is misleading.@Jonathan: No, it is not just IE. Using `mousedown` instead of `click` appears more responsive in all browsers due to the fact Alex pointed out.
Tom Bartel
@Tom Bartel: Ahh ok. I think I interpreted your answer incorrectly because of the context of the question. Re-reading it on it's own makes sense.Maybe you should add the following to the end of the sentence just to clear it up."The lag is apparent in all browsers due to the fact that a user has to make 2 interactions for 'onclick' as opposed to 1 interaction for 'mousedown'"
Alex