Optimizing Performance

There’s a number of ways to improve Mithril performance for the rare cases where pages are too complex for their own good.

First and foremost, you should think hard about whether performance optimization is truly your last resort. By nature, optimizing performance make aggressive assumptions that can break in edge cases and it yields difficult to understand code. As a rule of thumb, you should never implement performance optimizations if another solution is available.

For example, if you are building a table with thousands of rows and finding that the template is slow, you should first consider making the table show less items, because from a user experience perspective, no one is realistically going to scan through thousands of records. The effort to make the table paginated, searchable or filterable can improve the user experience in addition to solving the performance problem both on redraws and on initial page load.


Compiling templates

You can optionally pre-compile templates that use m() by running the template-compiler.sjs macro with Sweet.js. This step isn’t required in order to use Mithril, but it’s an easy way to squeeze a little bit more performance out of an application, without the need for code changes.

Compiling a template transforms the nested function calls of a template into a raw virtual DOM tree (which is merely a collection of native Javascript objects that is ready to be rendered via m.render). This means that compiled templates don’t need to parse the string in m("div#foo") and they don’t incur the cost of the function call.

It’s worth mentioning that Mithril has built-in mechanisms elsewhere that take care of real bottlenecks like browser repaint management and DOM updating. This optional compilation tool is merely “icing on the cake” that speeds up the Javascript run-time of templates (which is already fast, even without compilation - see the performance section on the homepage).

The macro takes regular Mithril templates like the one below:

  1. var view = function() {
  2. return m("a", {href: "http://google.com"}, "test");
  3. }

It pre-processes the m() call and replaces it with its output:

  1. var view = function() {
  2. return {tag: "a", attrs: {href: "http://google.com"}, children: "test"};
  3. }

Note that compiled templates are meant to be generated by an automated build process and are not meant to be human editable.


Installing NodeJS and SweetJS for one-off compilations

SweetJS requires a NodeJS environment. To install it, go to its website and use the installer provided.

To install SweetJS, NodeJS provides a command-line package manager tool. In a command line, type:

  1. npm install -g sweet.js

To compile a file, type:

  1. sjs -r -m /template-compiler.sjs -o <output-filename>.js <input-filename>.js

Automating Compilation

If you want to automate compilation, you can use GruntJS, a task automation tool. If you’re not familiar with GruntJS, you can find a tutorial on their website.

Assuming NodeJS is already installed, run the following command to install GruntJS:

  1. npm install -g grunt-cli

Once installed, create two files in the root of your project, package.json and Gruntfile.js

package.json

  1. {
  2. "name": "project-name-goes-here",
  3. "version": "0.0.0", //must follow this format
  4. "devDependencies": {
  5. "grunt-sweet.js": "*"
  6. }
  7. }

Gruntfile.js

  1. module.exports = function(grunt) {
  2. grunt.initConfig({
  3. sweetjs: {
  4. modules: ["template-compiler.sjs"],
  5. compile: {expand: true, cwd: ".", src: "**/*.js", dest: "destination-folder-goes-here/"}
  6. }
  7. });
  8. grunt.loadNpmTasks('grunt-sweet.js');
  9. grunt.registerTask('default', ['sweetjs']);
  10. }

Make sure to replace the project-name-goes-here and destination-folder-goes-here placeholders with appropriate values.

To run the automation task, run the following command from the root folder of your project:

  1. grunt

More documentation on the grunt-sweet.js task and its options can be found here