All posts by tim

Ahh Genesis

I’m hooked on slowing down classics!

So I was listening to bob.fm’s top nine at nine this morning. They pick a year and play nine songs from it.

Javascript Performance

So at work I had to fix the performance of an AJAX call. It was using a java library called AjaxAnywhere to completely replace a table. When it did that with thousands of rows it got REALLY slow.

It was simply replacing the table with innerHTML, and I figured if I changed it to using dom methods it would be faster. It didn’t due to what I’ll discuss in the third bullet point. But I learned a few other lessons about performance along the way.

1) Copy your array length to a local variable:

This is much slower:

for(var i = 0; i < someArray.length; i++) {
  // do something
}

than this:

for(var i = 0, il = someArray.length; i < il; i++) {
  // do something
}

2) Don’t use prototypes object creation functions in loops (I think prototype object creation works great for the small stuff, and is very legible). They’re too slow. I started out with this:

var tr = new Element("tr", {
  'class': 'unselectable',
  'id': idValue
});

but it turns out that prototype adding attributes via a hash (what Element.writeAttribute does) looks up the dom node each time, which is a performance drain. I changed it to this:

var tr = document.createElement("tr");
tr.className = 'unselectable';
tr.id = idValue;
 

3) Create a render queue. It took me a long to find this, but you’ll probably run into it no matter how fast your javascript runs. Generally web browsers wait until your javascript function exits to update the dom. If you’re adding thousands of objects your page can blank for seconds redrawing. What’s the answer? A render queue, which looks something like this (posting as a nice class as I don’t think there are enough examples like this):

var lgt = lgt || {};

lgt.RepetitiveTaskManager = function() {
};

lgt.RepetitiveTaskManager.prototype.renderQueue = Array();
lgt.RepetitiveTaskManager.prototype.rendererRunning = false;

lgt.RepetitiveTaskManager.prototype.enqueue = function(methodRef) {
  var renderQueue = this.renderQueue;
  renderQueue.push(methodRef);
   
  if(this.rendererRunning == false) {
    this.rendererRunning = true;
    setTimeout(this.renderer.bind(this), 1);
  }
}

lgt.RepetitiveTaskManager.prototype.renderer = function() {
  var renderQueue = this.renderQueue;
  var length = (renderQueue.length > 5 ? 5 : renderQueue.length);
  for(var i = 0; i < length; i++) {
    var functionCall = renderQueue.shift();
    functionCall();
  }
    
  if(renderQueue.length > 0) {
    setTimeout(this._renderer.bind(this), 1);
    return;
  }
  this.rendererRunning = false;
}


// first declare our renderer function
var callback = function(table, cellContents) {

  var tr = document.createElement("TR");
  var td = document.createElement("TD");
  td.innerHTML = cellContents;
  tr.appendChild(td);
  table.appendChild(tr);
};


// now add 1000 rows to a table

var taskManager = new lgt.RepetitiveTaskManager();

var table = document.getElementById("myTableId");
for(var i = 0; i < 1000; i++) {
  taskManager.enqueue(callback.bind(table, i));
}

Don’t forget bind is your friend in this case. For those without much experience with it, bind let’s you pass a function call with the appropriate variables to call the method with, without calling the method at that exact moment.

By using this render queue it will render 5 rows at a time on the page, and the table will seem to expand down the page as the user watches. This isn’t desirable in all case, but it’s generally better than having the user’s browser get unresponsive.

Twittering Boredom

waiting for the meeting

So there’s this aquaintance I follow on Twitter. Actually I’m not completely sure I’ve ever met her. But that’s beside the point. She likes to tweet incredibly boring status updates. Like “waiting for meeting to start”, “meeting ended”, “driving to meeting”. And I’ve realized that it’s somewhat contagious.

Much like that good friend you have, whose stories of drunken debauchery makes you feel like your own life is somehow less boring. Reading these tweets makes me feel like my life is somehow more boring. Does anyone else have this problem?

Dance is good for kids

We went to Stella’s first dance recital on Tuesday. Thankfully it wasn’t actually a dance recital. It was simply a chance for parents to sit in and watch a somewhat typical class. I think a dance recital for three year olds is somewhat cruel. Kind of like a spelling bee for the mentally challenged.

I was amazed though by what Stella learned. I’d always wanted my kids to take dance, because I feel like it’s something that’s much harder to pick up later in life. At UT I did a few dance related pieces and auditions. From talking to fellow auditioners, even people who had just a couple of years of dance as kids could generally pick up choreography about 10 times faster than I could. Which I don’t think is completely the lack of dance training’s fault. I’m a large force of nature that can’t always change direction on a dime (or a ball-change).

So I wanted Stella to take dance.

But what I didn’t count on, and why I think everyone should put their kid in dance, is that beyond the above reason is the focus and discipline. This was not some sort of old-school ballet class where you get kicked for talking or not being good enough. But it was very much about focus and discipline. A lot of time was spent making sure the kids were all where they were supposed to be and focused on what they were going to do next. It was fascinating to me as compared to organized sports. Sports are like dance in that they get rid of excess energy and teach cooperation and coordination. But I don’t think they’re good at teaching focus. I was never given tools for when I needed to stay focused out there in left field, and when I could goof off (and thus I pretty much always goofed off).

All in all, I was very impressed. Get your kids in dance. You’ll be really happy you did.

Reblog this post [with Zemanta]

A Helpful Guide to Your Position on Healthcare Reform

1) Check your investments.
2) Do you have investments? (Yes – goto 3, No – goto 10)
3) Do you have health care investments? (Yes – goto 4, No – goto 10)
4) Store your annual rate of return in field A
5) Check your health care plan.
6) Do you have a health care plan? (Yes – goto 7, No – goto 10)
7) Store your annual rate increase in field B
8) Is field B less than field A (Yes – goto 9, No – goto 10)
9) You have made a good investment and should be against health care reform.
10) You are for health care reform.

It’s interesting to me that the anti-tax group seems to be the anti-health care reform group. So they’re unwilling to pay anything for government services, but if it is a private service they expect, nay demand, to pay through the nose for it!

2009 Sunstroke 5k – #10

28:42 – I’m getting slower. I think I need to run with my iPod to speed things up. That was a really nice comfortable run. It was 87 and raining gently. I guess I need the 100+ degree heat to push myself.

I had an awesome run at Mabel Davis yesterday in the heat. I really felt like I pushed myself. Go figure.

Headbands rock!


I don’t care what Julie says. Headbands rock. I bought some on a whim at Target a few weeks back. Sure they’re designed to emulate the late seventies for kids who weren’t born then, but whatever.

Up until recently I was reticent to wear sunscreen cause that stuff stings when it rolls down your face into your eyes. And yes, I do wear waterproof. See waterproof sunscreen means it will stay on you after you get wet. It doesn’t mean that any that hasn’t bonded with your skin won’t roll down your face and get in your eyes.

Running with the headband was a revelation. My forehead stayed cool and dry and sweat was channeled down the sides of my head.

I can’t explain that visor on Stella’s head though. Kids these days.

The News Doesn’t Like Plot Twists

So there was a double-homicide in west campus a few days back. A guy and his girlfriend. Apparently they were really cool. The media seized upon it as this random horrific act… THAT COULD HAPPEN TO YOU!

Well it could happen to you if you deal large amounts of pot and you start leaning on a person who owes you thousands of dollars. So that came out today, but the Statesman’s still going along with their “poor kids, random act of violence story”.

Remember. Stay in school. Don’t do drugs.

Some Facebook Quibbles

Image via CrunchBase

So I’m going to try to keep this non-technical. But I do want to explain why Facebook will be eclipsed by something else fairly soon. Their programming interface is keeping Facebook from being used for anything but MafiaWars style games.

I’m currently writing a theater ticketing and management website called BuyPlayTix. It has a lot of cool features, and so I decided to start working on Facebook integration. I tried to do a Facebook widget about a year ago (remember back when they had those?), and got frustrated and gave up. I’m glad I did since they basically removed that feature entirely.

I went ahead this time since I knew there was at least one feature that I could add that would be useful.

So now you can create an Facebook event automatically based on your event in BuyPlayTix. Which is nice. It saves you a little bit of duplicate effort. You set up your ticketing website, click a button and your event is on Facebook. Except you still have to go to their website to actually invite anyone to the event. Because their API doesn’t support that feature.

I have a page for my theater company – Loaded Gun Theory. I have hundreds of fans who want to be updated about Loaded Gun Theory’s new productions. They have manually clicked a button to make this happen. Yet I cannot automatically invite those fans to Loaded Gun Theory events via Facebook.

Which was ok, I guess. I still added a feature that saves about 5 minutes of typing.

Next up, our mailing lists. Email is becoming increasingly outdated, we lose about 5% of our contact list every time we send out an update. I figured it would be nice to be able to email updates to people who have opted in to our mailing list, or update them via Facebook. So I wrote a widget that would let you lookup a person on Facebook. Only downside there is you can only look them up by full name. The API has a first_name field. And a last_name field. But you can only search by full name. Which stinks if you happen to be looking up one of the many women who have their maiden and married name as their full facebook name. Or you’re looking up anyone who has a nickname, but uses their full name on facebook.

Then I figured it would be nice to have an automatic import feature. Where it would take only those exact matches and update them with their corresponding facebook id. Thanks to the previous problems this wouldn’t be a great solution, but it would save some time. Guess what? Can’t do that. Even though I have a list of fans subscribed to my facebook page, there is no way to get them out via the API. That’s right. I can’t get a list of all the people who are fans of Loaded Gun Theory. Which is pretty stinking useless.

So I’m already irritated at this point. I decide to go setup the mailing list capability where it will send an email if that’s configured, or a message via Facebook if you’ve (manually) entered the users facebook ID.

I go to look at what sort of messages you can send. Turns out:

a) You can’t send a message FROM your page. I can’t send a message from Loaded Gun Theory. It’s always going to look like it’s coming from Tim Thomas. Which is great if you know me, but we actually do have fans who don’t.

b) You can’t send a message that shows up in the Inbox. At all. Period.

c) You can’t send a private message to a user.

What can you send? You send send a status update (again as Tim Thomas, not Loaded Gun Theory). You can send a notification (which shows up in that tiny little box in the right hand corner that you don’t pay attention to). Or you can respond to a status update. Which doesn’t seem to be the best way to notify a fan about a new production.

So what do I have? I’m still sending out email and I’m sending those little notifications in the bottom right. I guess it’s better than nothing. But barely.

I understand they’re trying to prevent spam, but they’re also preventing every legitimate use. And I can still go into the application and spam people. It just takes longer for their page to load than mine.

I also added twitter support. That took 3 lines of code, and works exactly as you would expect.

Has anyone had success in communicating with Facebook users via the API? What did you do?

Trying Tofu

Mmm…. tofu

Last night was the first time I cooked tofu. It turned out well.

Julie is having to adjust to being a parent. Previously she would occasionally head off to the cupboard to make a peanut-butter and jelly sandwich if what I was making was unappetizing enough. Now she has to sell it. We’re in the food sales business here these days. So she may have wanted to hurt me physically when she saw tofu on the menu. But then we discussed how tofu was in miso soup, and that was good. Everyone was on board.

As I cooked I had to pay tribute to Stella. She’ll try pretty much anything raw. She demands raw potatoes, ginger, and tofu. But once they’re cooked she often won’t eat them. And she detests anything green. I started giving her the curved parts of the red peppers to make my dices nicer, and now she demands them.

The dish last night was Spicy Tofu with Red Pepper stir-fry. Stella thought the tofu was better uncooked. So did I. But then she played one card too many. She said she liked the peanuts better uncooked. We notified her that the peanuts had been put on top of the fully cooked dish and were still raw. This led to perhaps two more bites.

There’s no logic with three year olds. I can’t really complain. Stella eats a lot of foods. And she’ll eat almost anything while I’m preparing dinner. She just doesn’t want it cooked.

Our pediatrician told us recently that we’re going to need to start Etta on solid foods. Probably this weekend. I get to try again with another child.

What is the weirdest thing your kid eats?