Sunday, November 28, 2010

A message to my children about lying, consequences, and responsibility.

"Lies may take you forward, but never back" - From a movie, original source unknown.
In other words, you might be able to use lies to gain things in life (items, power, status), but you can't take lies back and that makes them dangerous. Telling the truth is ideally the best choice, but once you've made a bad decision, it may not seem that way and that's why it's so important to make good decisions to begin with.

One [oversimplified] way to decide if a decision is good, is to see if you answer yes to more than 5 of the following tests:

Am I really willing to deal with any negative consequences?
Am I prepared to take responsibility?
Would it be good for me?
Would it be good for other people it effects?
Is it lawful?
Is it moral?
Is it ethical?

If you can only answer yes to 3 of these things, it's likely not a good decision.

consequences: Can be good and bad. They're the result or effects of some other action or decision. If you drop a rock on your foot, the consequences will likely be an injury, time lost dealing with the injury, medical expenses, time lost while healing, etc...  On the other hand, making a good decision can have good consequences. For example if you choose to save money and invest it well, you'll earn more money and that would be a positive consequence.

responsibility: Most simply put, "the ability to respond". Here in the US though, that's not how much people see it. For example, if you're walking down the street and see a man who is starving and you have enough food (or money to buy food) to spare, you have the ability to respond by giving him food. That sounds very simple, but as you get older, you'll find more and more reasons it isn't. On the other end of the spectrum you'll find people saying, "that starving man isn't my responsibility" because his situation isn't a consequence of my choices or actions (assuming that's true of course). I think we need to have a well conditioned "sense of responsibility" where we can find somewhere in between. Where we will respond when we have the ability, but we don't become overwhelmed by the world around us either.


Wednesday, November 3, 2010

How jQuery .css() performance could be improved.

I've had a need to modify the CSS style of MANY elements on a page (think MASSIVE HTML reports) for much longer than I've known about jQuery. The solution I found, I've only used with IE so far but I'm sure can be implemented with other browsers, is to use proper stylesheet rules to identify groups of elements, then use JS to edit the document.stylesheet itself. The benefit here is that the browser is internally optimized to parse the DOM and apply the styles whereas right now (to my knowledge), jQuery.css() parses all elements matching the selector and then applies the styles.

Obviously this can be a bit more work to setup, but if your pages are as large as mine are, the performance difference is significant. With jQuery.css() I've got updates that take 1.2 seconds compared to .2 seconds when modifying document.stylesheet[].rule[].style.value.

If jQuery.css() were to parse the document.stylesheet.rule collection and look for matching rules, then apply the new style to a rule when a match was found, jQuery.css() could be MUCH faster in many cases.

To take the idea a bit further, when a matching rule isn't found, one could be added to the stylesheet so the browser could then handle style updates, but this approach could get sticky due to rule precedence. The rule (selector) added might be less specific than a different, but applicable one and in that case, would not be applied where the current jQuery.css() approach would apply it since it would add/modify an inline style declaration.

In my search for "how to modify a document stylesheet using jquery" (after wasting much time with a certain arrogant op in #jquery who thought he knew better and should from what I'm told), I did find a great little jQuery plugin that goes a long way toward doing what I seek with jQuery and thus breaking away from browser-specific syntax, but taking advantage of the performance benefits provided by modifying an existing rule. That plugin (not heavily tested on my part, but at the least a great proof-of-concept) can be found at: http://adam.yanalunas.com/blog/archives/100

Instead of using something like:
document.stylesheet[0].rule[0].style.display = 'none';

You'd use something more like (where cssStyleRule is a stylesheet rule object with selector and style):
$(document).CSSSelection(cssStyleRule)
... to add/modify the style of a rule.

BTW, I don't claim to be a jQuery expert and have only been using it lightly for a couple years, but I'm about as new to HTML/JS/CSS as Steve Jobs is to firing the RDF weapon so I think I'm pretty well on track here. If you know different, please enlighten me (without flaming if you want to see your comments posted).

Followers