...

View Full Version : Check or just set boolean?



mattyod
06-16-2010, 12:24 PM
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:



myCheck: false,

myRepeatedlyCalledFunction: function() {
this.myCheck = true;

// do some stuff
}


vs



myCheck: false,

myRepeatedlyCalledFunction: function() {
if(this.myCheck === false) {
this.myCheck = true;
}

// do some stuff
}


thoughts?

mattyod
06-16-2010, 02:35 PM
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:



<?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>

Old Pedant
06-16-2010, 07:50 PM
So did you also test

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum