Parsing Booleans: What's best for performance?

General Discussion
  • Global Moderator Plugin & Theme Dev
    #1

    Most know that there are three ways of parsing booleans out of truthy / falsy values:

    var bool = !!value; // double bang
    ============================
    var bool = value ? true : false; // ternary
    ============================
    var bool = new Boolean(value); // Boolean constructor
    

    But which one of these is fastest? Well, according to this jsPerf, the ternary and double bang operational ways are almost exactly the same performance, and both are faster than the constructor method. So, you get to both gain performance and type less by using the double bang method above!

  • NodeBB
    #2

    We mostly use !!var, but filtering an array so you can get rid of falsy values with arr = arr.filter(Boolean) is neat as well 🙂


Suggested Topics


  • 0 Votes
    3 Posts
    1000 Views

  • 0 Votes
    2 Posts
    2538 Views

  • 0 Votes
    8 Posts
    3418 Views

  • 4 Votes
    2 Posts
    2282 Views

  • 1 Votes
    2 Posts
    1004 Views

| | | |