Faster Loading Through Eval()

written by admin

Note: Unfortunately, the images that accompanied this post were stored in an individual’s private share and have been lost. Because this post is very old, it will not be updated. However, the results of this work did make its way into SproutCore as SC.Module.

When you want to reduce the load time of a JavaScript-heavy web page (like any SproutCore application), you inevitably spend a lot of time thinking about how to reduce the time consumed by the JavaScript itself on page load.  This is particularly true when working on mobile devices, as the Gmail or Mobile team recently pointed out.

The Gmail team used an interesting technique to reduce their load time on the iPhone by loading scripts as comment which they later regexed and eval’d on demand.  I wanted to see if I could make something like this totally automatic in the next version of SproutCore, so I’ve been doing some research.  This post documents some of my findings.

Anatomy of a Script Load

Whenever your browser loads a script, it essentially has to go through three steps:

  1. Download the script from the server (if needed) or load it from cache

  2. Parse the script

  3. Execute the actual code in the script

Now, I should point out that usually 80% of your performance problem lie in step 3.  You can often cut the script-portion of your page load time the most by simply redesigning your code to do less work up front.

This is mostly what we focused on for SproutCore 1.0 and it yielded some big improvements.  One app, for example, had it’s worst case load time on IE drop from 10 seconds to 2 seconds just by using our new API that defers execution whenever possible.

After you stop doing more work than necessary, often the next biggest bottleneck is step 1.  You can get around this by packaging your scripts into as few files as possible, using a CDN, and using caching.  Again, SproutCore 1.0 makes almost all of this totally automatic.  So we’ve got you covered there.

Faster Parsing Through Eval()

Once you’ve beat these first two bottlenecks, the next thing we need to address is JavaScript parse time.  Believe it or not, your browser can actually spend a significant amount of time just parsing your code; especially if you have a lot of it.

You can get around this with some trickery, it turns out, by downloading your JavaScript, not as actual script, but as something else.  The Gmail team wrapped their scripts in comments.  I’m trying to simply download my scripts as strings.

Since strings and comments both don’t involve constructing actual code, the browser is able to process this code much faster on initial download.  Only later, when you actually need the code, do you need to eval() the code to get it ready.

So how much exactly can you cut your page load time if you download strings/comments instead of actual code?  Well, I ran a few tests to find out.

The Tests

In these tests, I eval()’d a 50K function directly.  Then I tried wrapping the same function inside of a comment and eval()’d that.  This roughly reproduces the technique described by the Gmail team.

Using comments to defer parsing is clever but it requires some hacks involving an iframe and it won’t work across domains, so I also tried wrapping the 50K function inside of a string as well.  Theoretically, passing your code as a string and processing it later should get us most of the benefits of comments without the limitations.

Here are the results of parsing in all the major web browsers on the desktop (times are avg/eval in msec):

I also tried the various methods on an iPhone 3G (not 3GS).  I am including a separate chart here because the raw times are so different I didn’t want to throw off the chart scale:

The Results

Overall, wrapping code as a large comment is faster than wrapping the code as a string on all browsers.  (Most of them required less than half the time to parse a comment over a string.)

However, on all browsers, even using the string approach was 3x-20x **faster** than loading the function directly.  That is a huge win if parsing time is your primary bottleneck. [Again - it usually is not.]

Overall, even though using comments is technically faster than strings, I think we are still going to go the strings approach for now.  The marginal gain is not large enough to counter the drawbacks of the technique in my book.

The Caveats

So, looks like perhaps we’ve found a nice way to further improve the load times on SproutCore apps.  I’m going to build this in at some point in SproutCore 1.1 or later.

That said, if you are thinking about using this technique on your own site, there is an important limitation you should consider.

This technique works by loading your code as a string or comment first. What is did not show in the graphs above is the added time required to convert this string or comment into usable code.  When you include this time, the actual time required to get your code into a usable form for either technique is actually four times slower than simply loading your code.  Not good!

Because of this, I would recommend using this method only if the following are true:

  1. You have already made your code do as little as possible on page load and packed your JS into the minimum number of files and you defer loading JS whenever possible.

  2. You need to reduce your initial page load time to an absolute minimum.

  3. You have a lot of code to load, but during any one session you may execute only a portion of it.  [In this case, be sure you can eval your code in bits and pieces]

Finally, if you are working on a mobile device, note that the gain here will be far more important as the time differential will be noticeable to a user.  This is the primary reason why we are adding it to SproutCore in fact.

Where’s the Code?

So nice technique.  How will this make it into SproutCore?  Turns out, there is already a prototype out there.

SproutCore 1.1 or later will adopt the new CommonJS SecurableModule standard for packaging JavaScript.  When we do this, our loader will be able to accept strings of module code as well as functions.  You can find our prototype of this code in our new loader, code-named Tiki, on Github.

Todos Tutorial: From 0 to fully working SproutCore app

written by admin

Link: Todos Tutorial: From 0 to fully working SproutCore app

Ever since we released SproutCore 0.9 in 2008, people have asked for a tutorial that takes you from zero all the way  through talking to a backend server.

Well, now we have one.  The Todos tutorial shows you how to build an app, add a model, add some views, hook up the controllers, write a simple server, and finally wire your app to the server.  It’s fun!

There is even a choose-your-own adventure portion where you can pick the backend technology of your choice for the server.

Please give the Todo’s tutorial and try and help us make it better.  The tutorial is a wiki so you can fix any problems you find right away.  Or leave comments.

We also need help adding support for more backends.  Currently the Sinatra is the only officially supported one.  Drafts exist for PHP and Merb, but they need to be updated.  If your favorite server tech is something else – add it.

Happy Todo-ing.

PS. You need the very latest SproutCore for this to work (1.0.1009 - that’s one GREATER than RC1 we released a few days ago).  Make sure you “sudo gem update sproutcore”  before you begin.

No Content Found

written by admin

sproutcasts:

> > Web performance gurus Gomez, Inc. just launched a new version of their point & click [website recorder service](http://www.gomez.com/products-solutions/technology/the-gomez-recorder/) and it’s running on SproutCore! > > > > Gomez is a B-to-B company so most of us can just fiddle with the app, unfortunately.  But if you’re lucky enough to work somewhere that uses Gomez, go give it a try.  The team there did some great work. > > > > (For the record: the Gomez Recorder is 0.9.  I’ve had some great input from them while preparing 1.0 so I hope they’ll be able to come along to the new platform soon.) > >

SproutCore Release Candidate 1 Hits the Streets

written by admin

Last night I merged the final set of changes for the first release candidate of SproutCore 1.0.  I also published a new gem (build 1.0.1008) so you can get the official release quite easily.  Just open your terminal [on Mac or Linux] and type:

`sudo gem install sproutcore`

Then enjoy!

If you just want a taste of what the release candidate can do, check out the demos at:

> > [http://demo.sproutcore.com](http://demo.sproutcore.com) > >

Especially try the SampleControls app, where you can see an example of over 300 views rendered on a single page (in the Controls tab).

What’s In the Box?

In case you haven’t heard, SproutCore 1.0 is a major revision of the entire SproutCore platform. New build tools, new data store, new view layer, massively upgraded bindings and property observing and more.  Our primary goals for this release were performance and stability.  I’m happy to report we achieved both in spades.

Fast, Fast, Fast!  Make it fast!

I created SproutCore in the first place was because I wanted to build fast applications on the web.  I believe the web is the ultimate application platform, but it will only be so when we can build apps that are just as fast and fluid as their native cousins.

While the 0.9 code I think proved this concept; we hadn’t really achieved parity yet.  Our goal with 1.0 was to make sure SproutCore apps could load on any desktop browser in under 3 seconds, even if the app is big and complex and has a large data set.

We passed this goal actually; by 50%.  Most SproutCore apps can load in under 1.5 second with the proper deployment.  You results may vary; but if they do it is unlikely SproutCore will be the bottleneck.

Here are just a few of the big enhancements we added to make this possible:

  • The build tools can now automatically combine your JavaScript and CSS to minimize the number of assets you have to download.  Usually, you’ll only need to get 2 scripts and 2 stylesheets that’s it.

  • Output apps are also designed for caching.  If you follow our deployment guide, your app will be able to essentially “live” in your user’s browser cache; eliminating network latency issues for the SproutCore part entirely [after first load].

  • We also made it possible to break your application into loadable bundles that can be deferred until you actually need them.  This means if you have a large app you an write portions of it so they only load when they are actually needed; keeping your initial page weight down to a minimum.

  • On the framework level, our view layer was rewritten specifically around the major performance bottlenecks in IE.  Our new layer can render 300 views client side in IE in about 100msec (on my Mac Book Air).  It’s even faster in the other browsers.

  • Once rendered, all views on the page are now displayed using absolute positioning.  This not only virtually eliminates cross browser issues for display - it also makes the browser actually render your content faster.

  • We also rewrote the datastore layer to “lazy evaluate” queries and data changes.  This means you can easily shove a large number of records into the data store - some people have been using 50,000 or more records each time their app loads - and we’ll handle it just fine.  You can load those records often in just a few hundred msec.

  • Finally, the code property observing and bindings layer that underpins all of SproutCore was massively optimized.  Notifications are sent less often, use less memory, and they are generally 3x faster than the 0.9 code.

All of these improvements essentially mean you can make just about any SproutCore app load and run in just a few seconds.  The only piece of your infrastructure we can’t control is the part of your server that delivers actual data.  If you can make that component blazing fast as well, your web app will knock the socks off of a native app any day.

If you want an example of this performance, just visit the Tasks app (running 1.0 Beta code on top of Google App Engine) and login as “guest”.  You can also checkout the new SproutCore demos linked below.

No More Changes!

SproutCore is a large framework.  It has over 25,000 lines of code.  We wanted to make SproutCore stable - meaning both the API we published would not change underneath you (or at least it would change in a controlled way) and that features we added would continue to work; even as we introduced new code.

We started on this goal by adding nearly 5,500 unit tests across the entire platform.  It’s a lot of unit tests.  Currently, on Safari 4 and Firefox at least, every single one of these unit tests pass green.  IE7 and IE8 each file 18 tests due to issues with the tests themselves rather than the code they test; we aim to have these tests fixed by the time 1.0 is final as well.

We also massively reworked the entire API, scrubbing it for consistency and cleaning up areas that were confusing or made it too easy to do things wrong or too hard to do things right.

This API review led to a lot of church and instability, ironically, during development which was an endless source of frustration for those few brave souls who were building on 1.0 before it was finished.  It was worth the pain, however.  The new API is easy to use and generally works well.

We will always want to polish bits of the API here and there, but I’m convinced that we have a clear path to evolve our code now without breaking your existing code in major ways again.

What’s Next?

The release candidate is out.  This means we are “done” with the development of SproutCore 1.0.  Please test it out if you can and send us feedback.  If you find a bug or want a change in, please still submit it.  We can take changes up till just before we release thanks to our unit tests.

The core team will be working on finishing documentation on the wiki, validating example applications, and updating the website for our final release.  We would also like to have a party in San Francisco for the release.

When these things are ready,  SproutCore 1.0 will be fast, stable, and easy to learn (thanks to the new docs and tutorials).  Then, finally - after a year of effort - we will be done.

Thanks!

Finally, I want to say a word of thanks to all of those who have contributed to the development of SproutCore over the last year.  I went back through the commit logs and its actually a pretty big list.  I can name 30 contributors and that doesn’t include the folks who send me patches now and then via email.

But I just want to call out those I can.  If I missed you, please forgive me.  But your contribution is really appreciated:

  • Billy Kakes

  • Brian Cully

  • Charles Jolley

  • Chris Hyle

  • Colin Campbell

  • Cortlan Klien

  • Erich Ocean

  • Evin Grano

  • Geoffrey Donaldson

  • James Austin

  • Jason Ketterman

  • Jonathan Lewis

  • Joshua Dickens

  • Juan Pinzon

  • Majd Taby

  • Martin Ottenwaelter

  • Maurits Lamers

  • Mike Ball

  • Mike Subelsky

  • Mohammed Ashik

  • Onar Vikingstad

  • Peter Bergstrom

  • Peter Wagenet

  • Ray Bodenhorn

  • Santosh Shanboque

  • Sudarshan Bhat

  • Tanner Donovan

  • Teresa Tsui

  • Thomas Langemann

  • Tom Dale

  • Trek Glowacki

Most of us get into the software industry to try to build something that is really, truly, great.  Like-magic kind of great.  It turns out it is really hard to do this.  We can turn out good software, but the really great stuff doesn’t come easy or often.

SproutCore 1.0 is really truly great.  It’s the best piece of code I’ve never been involved with and it’s thanks to the contributions of the folks above, as well as those who’ve been testing, providing feedback, and brave enough to try to build actual products on our code while it was still in development.

So thank you!  I can’t say it enough.

What’s Next Next?

Finally, I can’t leave this post on 1.0 without pointing you who are interested to some of the cool things we are already starting work on for SproutCore 1.1 (code named Quilmes).  Now that 1.0 is looking really solid, we get to spend some time on really fun things including:

  • An awesome new animation layer (already working in alpha form).

  • A FormView that can automatically generate an editor or browser UI from model objects

  • A UI builder - like Interface Builder or Atlas.  The engine is already finished; we’re just putting together the editor UI.

  • A new JS loader based on CommonJS SecurableModules.  This will make SproutCore usable in server-side code as well as making it easier to suck in external libraries for your own use.

Development is being tracked on the wiki so head on over to take a look.

The Tasks app I linked to earlier is also coming along nicely.  The team there has a few more changes lined up, but we will soon be able to use it as our default bug tracker and planning tool for SproutCore.

Last of all, I want to give a shout out to the team at Kiva.org who has been working with us on a new Kiva Loan Browser.  Kiva, you might know, is a non-profit that makes microloans to entrepreneurs in developing countries.  Microloans are one of the most effective ways to address poverty and Kiva is one of the best.

SproutCore needs a good end-to-end example application and so we’ve partnered with them to build a new Loan Browser - and perhaps more in the long run.  I hope that once SproutCore 1.0, you will help us build this app into a really great way for Kiva to find and share loans.

Help others while you learn!

So this post has turned into a minor book.  Sorry for the length.  1.0 is a big milestone.  I can’t wait to see what you build with it.

-Charles