Short-circuiting and Booleans

I am familiar with short-circuiting from other programming languages, where the second half of a boolean expression might not be evaluated if the first half evaluates to an expression sufficient to arrive at an overall value. Meaning, if the first half of an OR operation is TRUE (or the first half of an AND operation is FALSE), the second half is not evaluated.
Because statements also are expressions in Javascript, you can do something like this:

tokens.length && (aToken = tokens.shift());

What happens here is that the first half of the evaluates to a non-zero value, the right half of the operation is evaluated, which causes the first item to be pushed off the tokens array and shoved into the aToken variable. If the tokens.length is 0, no further evaluation is necessary.

Also, if the && expression above evaluates to TRUE, the value of the ext ire boolean expression is value of the last value in the list.


Today’s Weirdness: OR’s

I can understand using the logical test on an object to see if it is null.

if (str) {
// str contains a value


if (!str) {
// str is NULL -- or 'undefined'

But this is weird:

var d = a || b || c;

d will pick up the first non-NULL value of a, b, or c. If all three are undefined, so will be d.

I’m sure I will stumble across more info about the best use of this idea, but to someone used to dealing with binary results from Boolean compares, it is both intriguing and scary.

JavaScript append

I’ve started my JavaScript journey by creating a tool to parse localized string files for iOS. We often get these files back from the translator and there are minor errors in them.

The only problem is that Xcode will only flag as a file as “corrupt.” It won’t tell you what the error is or even what line the error is on. The most common error is a missing semi-colon at the end of the line, although mishandled quotes is also an issue.

Eyeballing this errors is hard because the strings are often in foreign languages with the non-American characters and the files are a couple of thousand lines long. So I’m writing a small web page to read a specified file and display it with the erroneous lines highlighted in red.

The issue I which I quickly ran into was the append function (+=) in Javascript is horribly slow.

// bad, don't do this, especially in a loop
results.innerHTML += '<div>' + newComment + '<br></div>';

I found a buffering function which really speeds up the process. This code was posted over five years ago, so perhaps there’s a better way, but this was very speedy indeed:

function StringBuffer() {
this.buffer = [];
StringBuffer.prototype.append = function append(string) {
return this;
StringBuffer.prototype.toString = function toString() {
return this.buffer.join("");

var buffer = new StringBuffer();
// processing loop
// do work to create newComment values
buffer.append('<div class="comment">');
// end of loop

// now, add the buffer all at once to the HTML
resultsDiv.innerHTML = buffer.toString();

Errors installing PhoneGap

I was trying to install PhoneGap on my work MacBook yesterday and the installation would fail at the end, just saying the installation failed and I might “want to try again.”

Cmd-L showed me the entire log file. It turns out that cocos2d had installed a template for Xcode and marked the templates directory as owned by ‘root’.

So I changed the owner of the directory:

sudo chown fraffic “/Users/fraffic/Library/Application Support/Developer/Shared/Xcode/Project Templates”

Although I suspect that changing the permissions on the directory would’ve also fixed the issue.

After chown’ing, the install worked.

Relearning JavaScript

Just got done watching video from Velocity 2011.┬áThe Best of Velocity – Know Your Engines: How to Make Your JavaScript Fast

Not a lot has changed in the realm of byte code run-time optimizations in the last ten years.

Things that can help the optimizer:

  1. Type invariance of arrays
  2. Non-sparse arrays
  3. Don’t use ‘eval’ or ‘with’ anywhere (and especially near your speed-sensitive code).
  4. Be aware of where you want optimal performance.