Shaving Bytes with JavaScript Booleans

By  on  

Developers are always search for ultimate way to create something with the least amount of code.  This, of course, is one of the reasons we use minifiers: to serve code as small as possible.  Of course this practice has numerous benefits, like faster download time, less storage consumption, etc.  One way that minifiers are able to shave bytes off of JavaScript code is changing the way booleans are used.

true === !0 // Save 2 chars

false === !1 // Save 3 chars

A few bytes of every true and false go away with the ! evaluation.  If you set one-letter variables names to those values, you may end up saving more.  Keep in mind I'm not telling you to do this in your source code -- minifiers like Uglify JS will do this for you.  Just something neat to know about though!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    Add Styles to Console Statements

    I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message: WARNING! Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS. Do not enter or paste code that you...

Discussion

  1. Glad you aren’t advocating coding like this directly. Can’t beat true/false for readability.

  2. Roman

    Coercions like Number to Boolean doesn’t affect performance?

  3. Maybe too simple of a test: http://jsperf.com/bool-num-test

    Looks like using !0 and !1 may be faster (in Chrome 35) but only marginally.

    • MaxArt

      Even if that’s true, and it’s not due to some statistical error, the gain is so small it’s not really worth it.

    • Those are noops anyway. I would be surprised if the JS engine just optimise them away at compile time.

    • didn’t just*

  4. Still prefer true/false for readability. As you said, it is better to let the Minifier to do this for us. It is better to keep the true/false in our code.

  5. I expect the gain to be lost as soon as the file gets gzipped. I am wrong?

  6. oresh

    You should also remember the bitwise operations like
    !~number, that returns true only for -1

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!