Wednesday, August 19, 2009
Oddities of Boolean evaluation in JavaScript
I had some naive notions about how JavaScript evaluated certain Boolean expressions. For example, I assumed that code like
I wrote the following test to help me identify other perilous JavaScript irregularities.
Read the squares as:
would always evaluate likeif (x) { ... }
I was wrong. For example,if (x == true) { ... }
evaluates to true, butif ([]) { ... }
evaluates to false.if ([] == true) { ... }
I wrote the following test to help me identify other perilous JavaScript irregularities.
Read the squares as:
if (y)
green
else
red
Most Interesting
[]
[] == true
[] != undefined
[0]
[0] == true
[0] != undefined
[1]
[1] == true
[1] != undefined
0
0 == true
0 != undefined
1
1 == true
1 != undefined
''
'' == true
'' != undefined
'1'
'1' == true
'1' != undefined
'0'
'0' == true
'0' != undefined
false
false == true
false != undefined
true
true == true
true != undefined
'false'
'false' == true
'false' != undefined
'true'
'true' == true
'true' != undefined
null
null == true
null != undefined
undefined
undefined == true
undefined != undefined
Double equal false things
[] == undefined
[] == null
[] == false
[0] == undefined
[0] == null
[0] == false
[1] == undefined
[1] == null
[1] == false
0 == undefined
0 == null
0 == false
1 == undefined
1 == null
1 == false
'' == undefined
'' == null
'' == false
'1' == undefined
'1' == null
'1' == false
'0' == undefined
'0' == null
'0' == false
false == undefined
false == null
false == false
true == undefined
true == null
true == false
'false' == undefined
'false' == null
'false' == false
'true' == undefined
'true' == null
'true' == false
null == undefined
null == null
null == false
undefined == undefined
undefined == null
undefined == false
Not equal false things
[] != undefined
[] != null
[] != false
[0] != undefined
[0] != null
[0] != false
[1] != undefined
[1] != null
[1] != false
0 != undefined
0 != null
0 != false
1 != undefined
1 != null
1 != false
'' != undefined
'' != null
'' != false
'1' != undefined
'1' != null
'1' != false
'0' != undefined
'0' != null
'0' != false
false != undefined
false != null
false != false
true != undefined
true != null
true != false
'false' != undefined
'false' != null
'false' != false
'true' != undefined
'true' != null
'true' != false
null != undefined
null != null
null != false
undefined != undefined
undefined != null
undefined != false
Double equal true
[]
[] == true
[] != true
[0]
[0] == true
[0] != true
[1]
[1] == true
[1] != true
0
0 == true
0 != true
1
1 == true
1 != true
''
'' == true
'' != true
'1'
'1' == true
'1' != true
'0'
'0' == true
'0' != true
false
false == true
false != true
true
true == true
true != true
'false'
'false' == true
'false' != true
'true'
'true' == true
'true' != true
null
null == true
null != true
undefined
undefined == true
undefined != true
Triple equal true
[] === true
[] !== true
[0] === true
[0] !== true
[1] === true
[1] !== true
0 === true
0 !== true
1 === true
1 !== true
'' === true
'' !== true
'1' === true
'1' !== true
'0' === true
'0' !== true
false === true
false !== true
true === true
true !== true
'false' === true
'false' !== true
'true' === true
'true' !== true
null === true
null !== true
undefined === true
undefined !== true
Triple equal false things
[] === undefined
[] !== undefined
[] === null
[] !== null
[] === false
[] !== false
[0] === undefined
[0] !== undefined
[0] === null
[0] !== null
[0] === false
[0] !== false
[1] === undefined
[1] !== undefined
[1] === null
[1] !== null
[1] === false
[1] !== false
0 === undefined
0 !== undefined
0 === null
0 !== null
0 === false
0 !== false
1 === undefined
1 !== undefined
1 === null
1 !== null
1 === false
1 !== false
'' === undefined
'' !== undefined
'' === null
'' !== null
'' === false
'' !== false
'1' === undefined
'1' !== undefined
'1' === null
'1' !== null
'1' === false
'1' !== false
'0' === undefined
'0' !== undefined
'0' === null
'0' !== null
'0' === false
'0' !== false
false === undefined
false !== undefined
false === null
false !== null
false === false
false !== false
true === undefined
true !== undefined
true === null
true !== null
true === false
true !== false
'false' === undefined
'false' !== undefined
'false' === null
'false' !== null
'false' === false
'false' !== false
'true' === undefined
'true' !== undefined
'true' === null
'true' !== null
'true' === false
'true' !== false
null === undefined
null !== undefined
null === null
null !== null
null === false
null !== false
undefined === undefined
undefined !== undefined
undefined === null
undefined !== null
undefined === false
undefined !== false
Labels: javascript
Performance of converting an integer to a string in PHP
We had a discussion at work about the fastest way to convert an integer to a string in PHP. The options were:
- Casting: e.g. (string)$i
- String concatenation: e.g. $i . ''
- String interpolation: e.g. "$i"
- Conversion with strval() e.g. strval($i)
- stringCasting: 0.744 s (e.g. (string)$i)
- stringConcatenation: 0.971 s (e.g. $i . '')
- stringInterpolation: 1.005 s (e.g. "$i")
- strvalConversion: 1.443 s (e.g. strval($i))
- <?
- function timeTrial($functionName, $i) {
- $start = microtime(true);
- $functionName($i);
- $totalTime = sprintf("%0.3f", microtime(true) - $start);
- echo "<h3>$functionName: $totalTime s</h3>";
- flush();
- }
- function stringConcatenation($i) {
- for ($j = 0; $j < $i; ++$j) {
- if ($j . '' === $i . '') {
- echo "This should never happen";
- }
- }
- }
- function strvalConversion($i) {
- for ($j = 0; $j < $i; ++$j) {
- if (strval($j) === strval($i)) {
- echo "This should never happen";
- }
- }
- }
- function stringInterpolation($i) {
- for ($j = 0; $j < $i; ++$j) {
- if ("$j" === "$i") {
- echo "This should never happen";
- }
- }
- }
- function stringCasting($i) {
- for ($j = 0; $j < $i; ++$j) {
- if ((string)$j === (string)$i) {
- echo "This should never happen";
- }
- }
- }
- for ($k = 0; $k < 4; ++$k) {
- $iterations = 1000000;
- foreach (array("stringConcatenation", "strvalConversion", "stringInterpolation", "stringCasting") as $test) {
- timeTrial($test, $iterations);
- }
- }
Wednesday, August 12, 2009
Backpacking Loop In the Crazies: Cottonwood, Glacier, Moose, and Campfire Lakes; Day 1
In mid-July, I and three friends went on a four day backpacking trip in Montana's Crazy Mountains. Originally, we'd wanted to head to Glacier Park, but bailed on that idea when we realized what a hassle getting the back country camping permits was. Instead, I went googling and found this guy who proposed a loop in the Crazies. It looked fantastic.
Sadly, Beartooth Publishing hasn't released a map of the Crazies yet, so we ordered a custom topo from mytopo.com, which was high quality and beautiful and huge for about $30. If you buy from them, remember to click through a discount link, such as the ones at Primal Quest.





We left work about noon and began the two hour drive to the Cottonwood Creek trail head. You might not expect that the drive from Bozeman would take two hours, but the last few miles of the road are in pretty rough shape and are best taken around 10 MPH. The parking lot was empty when we arrived. As we were unloading our packs, a car from Utah pulled up, two folks got out, and headed up the trail, which of course disappointed us as we expected to be alone.
In the parking lot, we weighed our packs. The lightest was just under 40 pounds; the heaviest, mine, was just over 50 pounds, including 3 liters of water.
As we headed out of the parking lot, we met the the folks from Utah who were headed back to their car. About three miles later we ran into this waterfall on Cottonwood Creek. Just before the waterfall, the trail becomes poorly defined and follows an old stream bed. We found that while the water was relatively high, it's best to stay on the downhill side of the creek and follow it upstream to a big log jam just downstream of the waterfall, which seems to be the easiest place to cross.
From the waterfall, the terrain ramps up and the trail hopscotches upward through an odd combination of exposed rocks and thick trees. There was still a fair amount of snow at places in the trees. As we neared the elevation of Cottonwood Lake, we came to a wide spot in the creek that I call Fake Cottonwood Lake since it had fooled me on a previous trip to Cottonwood Lake. The snow was still many feet thick in places there and where there wasn't snow, there was standing water or really spongy, soggy soil. Getting through there wasn't much fun. We got to Cottonwood Lake proper about 5:30. Originally, we'd hoped to make it over the ridge between Cottonwood and Glacier on the first day, but decided it was too late and set up camp at Cottonwood.


Sunday, August 02, 2009
Reflections on America
I enjoyed this piece by former BBC North America editor Justin Webb reflecting on what America is as he departed after 7 years in the States.
In more than seven years of life in America, I have come to value - to love, actually - the stolid, sunny, unchallenging, simple virtuousness of the American suburban psyche.
...
When selling a home in America, you have to pretend that you do not live there. No, you have to pretend that no-one lives there. Or ever
...
The English understand that we are all falling down. Dust to dust, we intuit. Americans do not. They have not got there yet.
...
And yet for all the ugliness, the deadening tawdriness of much of the American landscape and the tinny feebleness of many of its politicians - for all that nastiness and shallowness and flakiness - there is no question in my mind that to live here has been the greatest privilege of my life.
...
But if Sonia Sotomayor is to make it big, there must be something creating the drive, and part of that something is the poverty of the alternative, the discomfort of the ordinary lives that most Americans endure and the freedom that Americans have to go to hell if that is the decision they take. This is the atmosphere in which Nobel Prize winners are nurtured
...
I feel crazy going back to the old world. My five-year-old daughter Clara, who is the proud owner of an American passport, agrees. She says she intends to leave home, at around 12-years-old, and return to her native land. I do not blame her. If you are willing to chance your arm, if you back yourself, if you want to live the life, America is still the place to be.
...
Saturday, August 01, 2009
Project Euler Problem 5 One Liner
After sitting stumped for a while on Project Euler Problem 5, I came up with this one liner. That's probably cheating.
More Leif Wickland
Links
- My del.icio.us bookmarks -- a terse blog
- The up and coming, self-hosted, occationally up Wickland page
- Magistra Wickland enlivens language.
Archives
- September 2004
- October 2004
- November 2004
- December 2004
- January 2005
- February 2005
- March 2005
- April 2005
- May 2005
- June 2005
- July 2005
- August 2005
- September 2005
- October 2005
- November 2005
- December 2005
- January 2006
- February 2006
- March 2006
- April 2006
- May 2006
- June 2006
- July 2006
- August 2006
- September 2006
- October 2006
- November 2006
- January 2007
- May 2007
- June 2007
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- May 2008
- June 2008
- July 2008
- August 2008
- September 2008
- October 2008
- November 2008
- January 2009
- February 2009
- March 2009
- April 2009
- August 2009
- September 2009
- October 2009
- November 2009
- December 2009
- March 2010
- May 2010
- June 2010
- January 2011
- February 2011
- March 2011
- April 2011
- June 2011
- July 2011
- October 2011
- January 2012
- April 2012
- March 2013
- November 2013
- December 2013
- November 2014
- January 2015
- January 2016
- February 2016