jQuery optimalization

jQuery is an ideal Framework to quickly do interesting things with JavaScript. Recently I encountered a link where several optimization tips were being handled.

You can see it here.

JQuery Performance Tips And Tricks

Here is a short summary of the tips.

  1. Use the latest version.
  2. Use native JS where it’s better. So this.id instead of $(this).attr(‘id).
  3. ID and native selectors are the fastest.
  4. Child parent: parent.find(‘child’) is the fastest.
  5.  Caching: Cache an object $object = $(‘#id); and use .find to find the child elements.
  6.  Chaining: Chain operations on the same object where possible. Instead of invoking it constantly.
  7. Delegation: .live is good for simple scenario’s. Also use .delegate and .bind
  8. DOM is not a database. DOM insertion is costly. Limit adding to the dom to once occurence per action.
  9. Use .detach()
  10. Use .data(‘#id’, key, value);
  11. Avoid too many loops.
  12. $.fn.text is faster than $.text
  13. DRY code. The less repetition the better.
I recommend checking out the movie for a good overview.

Do more with less

In the world of webdevelopment building websites can be a tediously long job. Purely because a lot of techniques are dated and require a lot of boring repetition just to get them done.

This sometimes leads to hard to maintain code especially in the web because of closing tags and no requirements of indentation. But with modern technologies I’ve found newer techniques that make writing code a huge joy. Here’s a short overview of the ones I currently use.

SASS

First one I found was SASS a pre-compiler for CSS. CSS is used to make websites look pretty. On it’s own it’s already an abstraction away from inline markup in HTML. But SASS goes a step further. First of all it uses Python syntax which is indent dependent, which forces readable code. Secondly SASS allows for semi coding which makes repetitive code or hunting for that one color a thing of the past.

Example:

$blue: #3bbfce
$margin: 16px

.content-navigation
    border-color: $blue
    color: darken($blue, 9%)

.border
    padding: $margin / 2
    margin: $margin / 2
    border-color: $blue

Results in:

/* CSS */

.content-navigation {
    border-color: #3bbfce;
    color: #2b9eab;
}

.border {
    padding: 8px;
    margin: 8px;
    border-color: #3bbfce;
}

On the site of SASS there are more examples of what’s possible. Though it can go into extremes.

HAML

The next step is doing something similar for HTML in the form of HAML. Originally this was designed for Ruby on Rails, but it works quite well with just HTML or templating engines like TWIG. Greatest advantage is that closing tags aren’t written anymore and like SASS it uses indenting meaning code will always be nicely indented.

%div#things
    %span#rice Chicken Fried
    %p.beans The magical fruit
    %h1.class.otherclass#id La La La

To:

<div id='things'>
    <span id='rice'>Chicken Fried</span>
    <p class='beans'>The magical fruit</p>
    <h1 class='class otherclass' id='id'>La La La</h1>
</div>

CoffeeScript

De laatste implementatie die ik gebruik is CoffeeScript een precompiler voor JavaScript die ook de Python syntax hanteerd. Dit geeft het zelfde effect, maar maakt ook andere interessante dingen mogelijk zoals (maakt gebruik van jQuery):

The latest implementation I use is CoffeeScript a pre-compiler for JavaScript which also uses the Python syntax. This gives the same effect of readable code, but also gives other very interesting advantages. (example uses jQuery):

formValues = (elem.value for elem in $('.input'))

This one line results in the following code:

var elem, formValues;
formValues = (function() {
    var _i, _len, _ref, _results;
    _ref = $('.input');
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        elem = _ref[_i];
        _results.push(elem.value);
    }
    return _results;
})();

A huge difference.

Usage

There are several options of utilizing these pre-compilers, among which generating the code on the fly. Personally I chose for local compaliation into the relevant CSS, HTML and JS files. Using VIM I have a button I press that handles the generation for me. In later articles I’ll go into detail in how I set these up.

The only thing I haven’t found yet was something similar for PHP. Mainly because there isn’t a need for it in the coding space. Personally I’d only need actual code and no HTML escaping. However I do have a snipmate implementation for Vim which makes code completion very easy and convenient.

It’s important to reduce repetition and tedium in the jobs. And every year one should review how you do things and see how you can do them better.

Businesscards

There you are at a small scale event surrounded by fellow creatives without business cards. With enough excuses to not have them with you. They were old, dated, had the old logo etc. All relevant, but being at an event like this without business cards is killing for your networking possibilities.

Such an experience does motivate one to quickly remedy the situation. But it does generate some fun programs. What do you put on a business card?

Obviously the business name and contact info are important. Looking at other cards one sees addresses listed. But without an address to receieve people at and possibly moving around an address isn’t useful. So I settled on just a phone number and email to give them a way of contacting me.

But with that a card isn’t done. It should draw attention but also tell them what I do. And I do a lot. Not just websites and design. I also do illustration and several other things. But even then cards are usually handed over silently and then put away. Usually never to be talked about again.

In general it’s advised to have a card that draws attention without being a nuisance. Such as odd sizes or weird shapes and what not. But one doesn’t want to draw attention for attention itself. But just a boring card with info isn’t interesting either. And certainly not me.

These are all problems I had when I created the card. Most notably how do I make it not boring. All the base info was there but the card was boring. Reflecting on my personality and something of simple attention I had an idea for my card. So without further ado here’s the card I created.