Chili 1.8 Released Today

UPDATE: Chili 1.8a has been released

Changes
  • Added chainability
    • $( selector ).chili( options )… should work fine with almost all ChiliBook options
  • Improved speed
  • Improved examples
  • Added an automatic option, by default set to true
    • use true for executing Chili on page load
    • use false for not executing Chili on page load
  • Integrated the metaobjects plugin
  • Exposed the metaobjects selector by means of the metadataSelector option, by default set to object.chili
    • you can now use a different selector, but the param’s name still needs to be chili
  • Improved the elementPath / elementClass feature by means of the new options automaticSelector / codeLanguage
    • automaticSelector is a better name for elementPath
    • codeLanguage is a function that gets the element to highlight as an argument and returns the language to highlight it with
    • elementPath and elementClass are still supported
  • Fixed a bug that made Chili highlight more code sections than requested in dynamic setups, under special circumstances
Files
  • download all in a zip
  • read the manual
  • Examples
    • bundled languages
      this page shows how Chili highlights the bundled languages: JavaScript, PHP, MySQL, XHTML, Java, C++, C#, Delphi, and LotusScript (BTW, using the dynamic and automatic setup)
    • static, automatic, adhoc: this page shows how to setup Chili for
      • downloading recipes and stylesheets all at once, using HTML
      • highlighting code sections automatically
      • highlighting code sections ad-hoc
    • dynamic, automatic, adhoc: this page shows how to setup Chili for
      • downloading recipes and stylesheets as needed, using AJAX
      • highlighting code sections automatically
      • highlighting code sections ad-hoc
    • dynamic, automatic, adhoc, metaobjects: this page shows how to setup Chili for
      • downloading recipes and stylesheets as needed, using AJAX
      • highlighting code sections automatically
      • highlighting code sections ad-hoc
      • highlighting code sections using special recipes and stylesheets, by means of metaobjects
    • all the other combinations: this page shows all the other possible combinations

Optimize using dictionaries

There is a jQuery plugin for simplifying access to a select box. It’s very useful and I tried to use it in a prioject where I had to show hundreds of options at once. It worked pretty well with a few options, but as soon as those many options started to come in, performance fell down.

Let’s have a look at the implementation of the addOption function.

{[.addOption_original /enzymes/chili-js.php]}

Off Topic: The code above is a bit complex at first sight. The reason it seems complex is due to unneeded recursion, so common among jQuery developers. In fact the jQuery library itself makes use of unneeded recursion whenever possible…

Look at the snippet below the comment // loop through existing options. What’s going on here? A select box is treated like a database table where an option is a record and its value is the primary key. When an option is added to a select box, the everlasting dilemma arises: Is it an INSERT or an UPDATE?

The solution implemented here is to loop through existing options and break if a match is found between the new option value and the old ones. Sadly enough, this is the optimal (linear length) solution if you are adding just one option, but it’s the worst (quadratic length) one when you are adding many options at once.

Here is a replacement using a dictionary for storing and retrieving existing options. It’s an inner refactoring with no change on the interface, so it’s pretty simple to copy and use it in your own projects.

{[.addOption_replacement /enzymes/chili-js.php]}

The performance improvement is huge. Here are some figures I got on my PC by means of the Firebug Profiler.

  • all INSERTs: adding 1000 options to an empty select box needed 15234.375 milliseconds using the original code and 234.375 milliseconds using the replacement code, thus accounting for a 65 times improvement
  • all UPDATEs: replacing all the 1000 options of a select box needed 16578.125 milliseconds using the original code and 796.875 milliseconds using the replacement code, thus accounting for a 20 times improvement

If you want to test it by yourself here is the simple page I used:

{[.testpage /enzymes/chili-web.php]}