Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts

    Question Check or just set boolean?

    This is perhaps a somewhat trivial point but it's bugged me a couple of times recently and I wondered if anyone out there had any thoughts on the matter.

    Essentially I have an action that can happen many times but I want to record whether it has happened at least once. I know we are talking about a trivial amount of processing and code here but is there any real difference between the following two methods:

    Code:
    myCheck: false,
    
    myRepeatedlyCalledFunction: function() {
         this.myCheck = true;
    
         // do some stuff
    }
    vs

    Code:
    myCheck: false,
    
    myRepeatedlyCalledFunction: function() {
         if(this.myCheck === false) {
              this.myCheck = true;
         }
    
         // do some stuff
    }
    thoughts?

  • #2
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    I don't seem to have caught your geek inquisitiveness so I'll "answer" my own question...

    Well I threw the question out at work as well and the general feeling there was that using "===" to check both type and value of the Boolean was probably a slightly higher overhead than simply setting it.

    So, we built a little test page to check performance by running "==" and "===" checks as well as simple set 1million times in for loops.

    The results were actually a little surprising.

    Both Firefox and Chrome showed very minimal difference between the "==" and "===" checks.

    However, direct setting of the value took a little bit longer (220ms vs 270ms) in Firefox but was significantly faster in Chrome (95ms vs 16ms).

    So I think I'll probably go with direct setting as it also reduces the code wieght by that tiny bit

    Source from test page if you are interested:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>To check or to simply set</title>
        <style type="text/css">
            body {
                background:#ccc;
            }
            .content {
                width:600px;
                margin:30px auto;
                border:1px solid #333;
                background:#fff;
                padding:20px;
            }
            li {
                line-height:2em;
            }
        </style>
        <script type="text/javascript">
            var check = false;
            
            function checkSet() {
                var count;
                
                console.time('check 1');
                
                for(count = 0; count < 1000000; count++) {
                    if(check == false) {
                        check = true;
                    }   
                }        
                
                console.timeEnd('check 1');
            }
            
            function checkSet2() {
                var count;
                
                console.time('check 2');
                
                for(count = 0; count < 1000000; count++) {
                    if(check == false) {
                        check = true;
                    }   
                }        
                
                console.timeEnd('check 2');
            }
            
            function justSet() {
                var count;
                
                console.time('check 3');
                
                for(count = 0; count < 1000000; count++) {            
                    check = true;            
                }        
                
                console.timeEnd('check 3');
            }
            
        </script>
    </head>
    <body>
        <div class="content">
            <ul>
                <li><a href="javascript:checkSet()"><strong>Check 1:<strong> I check then set (==)</a></li>
                <li><a href="javascript:checkSet2()"><strong>Check 2:<strong> I check then set (===)</a></li>
                <li><a href="javascript:justSet()"><strong>Check 3:<strong> I just set</a></li>
            </ul>
        </div>
    </body>
    </html>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    So did you also test
    Code:
    myCheck: false,
    
    myRepeatedlyCalledFunction: function() {
         if( ! this.myCheck ) { this.myCheck = true; }
    }
    That seems a lot more logical, to me, then *either* doing === false or == false.

    Mind you, I would have instinctively *NOT* made any test at all. A really good optimizing compiler (esp. a C++ or Java compiler) would recognize that the "if" check is pointless and optimize it out of existence. [In fact, if you knew that the value in question was, say, a hardware register and you knew that the "if" was needed for proper hardware operation, you'd have to tell the compiler *not* to optimize it away.] Knowing that, I wouldn't have bothered to put it in there in the first place. But, of course, we don't expect to see "optimizing compiler" in the same sentence with "javascript", so your results don't surprise me too much.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •