- Chris Heilmann’s YUI based approach (136ms)
- Chris Heilmann’s DOM based approach (247ms)
- Stuart Langridge’s generated-toc (258ms)
- Josh Johnston’s solution with jQuery (781ms)
- My own HTML5 Outliner (3099ms)
- Chris Heilmann’s RegExp based approach (4440ms)
- Diogo Vincenzi’s solution with jQuery (9334ms)
The numbers above are based on an 1.5Mb file, which has some 200 headings to use for the TOC table. The actual file, was created out of HTML4 specification part on forms, to reflect a more natural usage. The results were a bit different, when I used my own generated file, which had a lot more headings, and a lot less text.
The test runner itself would create an
iframe, load the test document inside, inject the required script and call it. The source is available on Google Code (though be aware, that you will need to procure and hack the dependencies yourself).
I ran it on a decent 2.2Ghz laptop, using a vanilla version of Portable Firefox 3.5. With some exceptions (see below), the results were comparable and not unexpected in other browsers.
- The RegExp based approach was a non-contender from the start, as it doesn’t really work under any browser, other than Firefox.
- Diogio’s solution scored so low because it’s querying the DOM twice inside every loop iteration (I think).
- I am impressed at how well YUI3 performed – on every browser (incl. IE8), except Opera. I seriously need to follow up on the Opera thing…
- Speaking of YUI3 – one needs to go through extra hoops to measure performance, due to YUI loader’s
setTimeout(). And make sure that you have all dependencies in on file, to avoid calls to the Yahoo’s CDN.
- IE8 didn’t like 2Mb documents inside an
- I have plenty of space for improvement – it will be fun!