Convenience is rarely economical. Shredded cheese tends to be more expensive than a block of the same type. Pre-chopped vegetables are the same way. But when you spend that little extra, you gain that back in time that you don’t have to spend doing the prep work. Technology doesn’t always work that way. I recently encountered a scenario where using the “easier” method actually cost a shit ton more in CPU cycles.

Let me set the stage, I’m working on a system that generates static sites. It uses jade for the templates because it’s faster to write out than raw HTML so the development cost is low. This sounds great in theory but this development speed comes at a cost. You see, converting the jade templates to HTML isn’t exactly the fastest thing in the world. This additional templating layer also happens to be entirely optional.

When I say the conversion isn’t fast, I mean it was taking upwards of 18 minutes to generate a site with roughly 1500 pages from the jade templates. I knew damn well that it could run faster. My first thought was that the issue was jade and that perhaps I should be using something like ejs or one of the many other templating enginesthat exist in node land.

That thought process was quite flawed because at the end of the day I’d still be adding an unnecessary abstraction layer to the system. My solution? It would take a bit more time to code, but I figured simply assembling my HTML string in node would be the best way to solve the problem. My approach was to build out a string programatically:

var html = '';

html += [
  '<div>',
  '  <h1>Title</h1>',
  '</div>',
].join("\n");

You get the idea, nothing sophisticated at all. I prefer to work with arrays because it allows me to simulate multiple line strings with minimal effort. It took a bit of time to port over the jade templates, but it was worth it. Remember that I said the site was taking upwards of 18 minutes to generate? The new “raw JavaScript” method was taking a mere 4 seconds to do the same amount of effort. Yes, it was processing in less than 1% of the jade version.

Convenience always comes at a cost, and sometimes that cost isn’t monetary. I’m not saying you shouldn’t use jade or any other templating system, but you should understand the ramifications of your choices. Depending on what you’re doing those choices could have a serious impact on your user or, like my scenario, your server load.

Have you implemented a hack that allowed you execute code in 1% the time as before? Would love to hear about your incredible hacks, comment below!