Prototype and speed

Since Mike doesn’t really blog anymore, I figured I would steal his thunder and discuss some of his findings the past week.

For those of you that haven’t used Prototype before, it is a Javascript library which we use fairly heavily at webmail.  It has served us well for a while now …..

But version 1.5 is becoming too slow to be used in webmail. Being a very large web application, we don’t always have the luxury of elegant code, especially in javascript. Sometimes, we just have to do what works fastest and looks the ugliest. This past week Mike made some interesting findings, singling out some of the major performance culprits in Prototype.

Enumerable.each()

We have known this function is slow and have tried to avoid using it, but Prototype itself uses it on some very common functions such as:

  • Element.addClassName()
  • Element.removeClassName()
  • Enumerable.* (Basically all of them use each() somewhere)

Mike overrode the first two functions to use a regular loop instead of each() and saw a 200ms login improvement! That is a pretty large improvement, nearly 14% of the total javascript rendering time on login.

Element.update()

For a long time, I thought Element.update() just abstracted element.innerHTML. I was wrong. It calls stripScripts() and evalScripts(). Since we never pass in text containing script tags, we overwrote this function to just use element.innerHTML. Again, another huge performance enhancement.

Unnecessary abstractions

Prototype has added some great functionality to javascript, but sometimes they take it a little too far with their abstractions. Some functions serve very little purpose other than to add an extra function call.

  • Element.update() – Since we have taken out the stripScripts()/evalScripts(), there is really no reason to use this function
  • Element.setStyle() – As long as you don’t set opacity or float styles, don’t use this function.
  • Element.addClassName() – If you know an element doesn’t have the class name already, just call element.className += ‘ foobar’.

A better web framework – Part 1

As I have mentioned before, design patterns are one of my favorite areas of computer science. Well, design patterns have a close association with web frameworks, so web frameworks are definitely another favorite area of mine. They both help speed up the development process by providing a structure for applications. Since Ruby on Rails took off a couple years ago, other web frameworks have sprung up in many other languages. None of these have met my needs at Webmail.us.

The webmail application I work on is very different than many other Web 2.0 applications, which is why most of the existing frameworks don’t work for us.

  • There are no page refreshes and therefore:
    • Most drawing is done by Javascript
    • All data transfered is JSON encoded
  • Email, Calendar, Tasks, Contacts, Settings are all combined into 1 application which means:
    • Large memory footprint
    • Javascript performance very important
    • On the fly loading of HTML, Javascript, and CSS is a must

For the past few months, I have been thinking about what I really need in a web framework. Just like Rails, an MVC framework would need to be the base. In Part 1, I will just talk about the MVC foundation for this framework.

Model

An ActiveRecord type object would provide the MySQL data abstraction. I have mentioned that I have been working on my own implementation for a while, so I won’t go into too much detail on this. Basically, for a complicated web application, most existing model abstractions don’t work very well. With complicated JOIN’s, WHERE’s, and subquerys, something different must be used.

View

A templating mechanism must exist to have a complete web framework. Current view abstractions won’t work in webmail. Since webmail never refreshes, going to the server every time we need to populate a template is troublesome. The templating mechanism in the better web framework would allow a template to be used in PHP or in Javascript.

If a template is used in Javascript, once it is downloaded once, it wouldn’t have to be downloaded again. The hard part about a Javascript template is the inability to have loops and the need for slow regular expression parsing.

If a template request in Javascript has to make a server request every time a template needs to be populated, speed becomes an issue. This is fine for large templates, but what about small widget templates? Making a server request for a small element increases 1. latency and 2. server load which in turn increases latency even more.

Controller

Controllers are the backbone of a web framework. I like how Rail works with a controller class where each data request is mapped to a function call. This seems effective to me and needs little modification.

Conclusion

I know what you must be thinking: Your MVC design isn’t that much different than Rails. Well, that’s true, but remember this is part 1. The MVC design is just the foundation of a better web framework. Rails is popular because it is useful, so it is definitely a good starting point for developing a new framework.

Shout out to Mike for his work on Javascript templates.

Gnome vs. KDE and Joel on Software

Since I converted to Ubuntu a few months ago, I have wrestled with the decision of KDE vs. Gnome.  KDE has infinite configurability, while Gnome tries to be much more simplistic.  I like many of the KDE applications, like Amarok, Krdc, and Kaffiene, so I thought I would try to switch from Gnome to KDE.

This lasted about 3 hours.  I spent all 3 hours trying to configure KDE to my liking, to basically mimic Gnome.  After frustation set in, I quickly uninstalled and still use Gnome with a few of the KDE apps as well.

Today I started reading Joel Spolsky’s User Interface Design for Programmers and the first 3 chapters explained exactly why switching was so hard.

  1. Choices – KDE has so many it becomes daunting to try and configure
  2. Expectations – I expect KDE to behave like Gnome, because that is what I am used to.  This isn’t the case though.

I’m sorry Linus, but I don’t foresee converting to KDE anytime soon.  Not because I dislike it, but because its not what I know.

The best web framework yet

Ever since Ruby on Rails, every other web development language has attempted to develop a similar framework.  Normally I don’t like them because they try and copy RoR, which can be hard because of Ruby’s differences with most other programming languages.  But, I finally found one I will back.

PythonOnPlanes is awesome.  Not because of what it can do, but because its name is amazing.  I have never really coded in Python, but I think I am going to start very soon.