Performance Tuning

So I’m deep in the middle of some performance tuning work for the release of Coherent 3.0. Everyone knows that optimising Javascript is a bit of a black art, but I almost think I’m doing something wrong.

Here are the performance metrics for Coherent 2.0 running on IE 8 (in a VM):

      direct:  5.688s for 50 iterations  (Avg: 113.755ms)
     derived:  6.086s for 50 iterations  (Avg: 121.720ms)
     methods:  9.793s for 50 iterations  (Avg: 195.860ms)
      notify: 10.946s for 50 iterations  (Avg: 218.910ms)
notify class:  6.785s for 500 iterations (Avg:  13.570ms)

Coherent’s never been a stellar performer in Internet Explorer. That’s something of an understatement, but I’ve always felt the blame rested largely on IE’s craptacular Javascript engine.

Now, I’m not so sure.

Below are the same run times for the same performance tests, but this time the tests are running against the new optimised code in Coherent 3.0:

      direct:  4.309s for 50 iterations  (Avg:  86.170ms)
     derived:  0.625s for 50 iterations  (Avg:  12.500ms)
     methods:  0.910s for 50 iterations  (Avg:  18.205ms)
      notify:  8.356s for 50 iterations  (Avg: 167.110ms)
notify class:  4.547s for 500 iterations (Avg:   9.094ms)

Clearly, I’m doing something right. All the unit tests still pass. And I’m getting a 9.7× speed improvement for derived property access — that’s when accessing properties of a class derived from coherent.KVO, a 10.7× speed improvement for calling getter and setter methods, and significant 1.3× to 1.5× improvement for the other operations.

The same code running in other browsers (Safari 4.0, WebKit nightly, Firefox 3.5 and Firefox 3.6) all see speed improvements between 1.3× and 2.0×, but nothing nearly as dramatic as IE.

Below are the performance numbers from WebKit nightly running Coherent 2.0:

      direct: 304.750ms for 100 iterations  (Avg: 3.047ms)
     derived: 324.750ms for 100 iterations  (Avg: 3.248ms)
     methods: 286.250ms for 100 iterations  (Avg: 2.862ms)
      notify: 742.750ms for 100 iterations  (Avg: 7.428ms)
notify class: 257.750ms for 1000 iterations (Avg: 0.258ms)

And WebKit nightly running Coherent 3.0:

      direct: 195.500ms for 100 iterations  (Avg: 1.955ms)
     derived: 204.000ms for 100 iterations  (Avg: 2.040ms)
     methods: 213.500ms for 100 iterations  (Avg: 2.135ms)
      notify: 468.750ms for 100 iterations  (Avg: 4.688ms)
notify class: 162.500ms for 1000 iterations (Avg: 0.163ms)

This is just the result of changes to the core methods of coherent.KVO and coherent.KeyInfo. Of course, there’s still more performance tuning to come. But I don’t think I’ll have anything this dramatic to report again…

Resurrected Old Content

As part of getting the new coherentjs.org Web site ready, I dragged out a back up of the original content. The import for Wordpress went relatively smoothly, but it’s pretty clear this content either needs to be cleaned up or rewritten.

The biggest problem is that a lot of it is simply not accurate any more. However, I’m going to leave it there for historical interest — I’m a bit surprised reading about my first efforts back in 2005. Maybe it will interest other developers too.

Probably the most important feature of the new site is the API documentation. This is greatly expanded from the original documentation from Coherent 1.0. And it will only get better as I continue to review the code and add doc comments.

Declarative Syntax for Child Widgets

One of my goals for Coherent 1.1 is the option of using a declarative syntax to set up child widgets. This would greatly simplify the average init method and make the code a bit clearer and easier to understand. Read More

Favourable Reception for Coherent

I recently announced the upcoming release of Coherent 1.0 and I’ve been very pleased by the positive reception the library has received.

Lots of folks have come out of the woodwork to either say they’ve been looking for something like this for ages. I guess there are more fans of the Apple development model than I thought. And obviously a lot of us build Web applications either for a living or in our spare time.

As I’ve been working on some features I’ve planned for 1.1, I’ve run across some strange problems with selectors which seems to tie into John Resig’s recent blog post about selectors.

Selectors and Bindings

One of the routine complaints about Coherent (yes, I’m talking about you Ryan) is the use of custom attributes. It seems that some people like their HTML pure, like it was back in the old days. Read More

Coherent 1.0 Release Candidate 1

I’ve been working somewhat furiously to get Coherent ready for its first release. There’s already code in the wild on a heavy-traffic e-commerce site that is using it, but there is still a bit more testing I’d like to do. And the documentation could use another brush up.

If you’ve downloaded either the ZIP file or pulled down the SVN repository, you should probably update to get the latest bug fixes and other goodies.

Properties And Bindings

Possibly the two most important concepts in the Coherent library are properties and bindings. If you’re familiar with modern programming languages, you’ve probably run across properties before, but bindings may be new unless you’ve worked with Apple’s Cocoa library. In order to get the most out of Coherent, you’ll need to understand these two facilities. Read More

Faster than a Locomotive?

After squashing a couple nasty bugs this weekend, I got to thinking about the performance implications of my solution. In a nutshell, the solution required wrapping getter methods with code to establish the ownership link between the value and the object. This wrapping only occurs for properties that are observed or part of a dependent key relationship, but still, we’re talking an extra layer of code. Read More

New Tutorial on Writing Widgets

As the release of Coherent looms nearer, I’ve been encouraged to start writing some documentation. My first effort is up: Writing a Widget. This tutorial takes you through creating a widget using Coherent starting with raw mark up and ending with a functional (if simple) widget. Thanks to some great feedback (and encouragement) from Neil Mix of Pandora, this tutorial doesn’t suck.

Oh, and if you haven’t tried Pandora yet, what the heck are you waiting for? This is one of the coolest things I’ve run across in ages. Of course, now my wallet is going to be completely empty because I’ve been turned onto a half dozen musicians I’d never heard of before…

Writing a Widget

Coherent strives to present a sane Model View Controller paradigm for client-side Web development. So writing a Widget using the Coherent library may be a bit different from what your accustomed to. However, if you’ve ever written desktop software, this should feel right at home.

For this tutorial, we’re going to build a simple photo gallery to display a static list of images with captions. In a future tutorial, we’ll see how we can expand this sample to pull image information from Flickr or other sources. Read More