Javascript Optimizing for Speed

I’m in the process of developing a CPU intensive Javascript game. I found the profilers of the different browsers lacking and needed some higher resolution probing of CPU hogger sections in the code. To this effect I created the timers manager below. Basically at any point in the code where you want to start measuring something, say data sorting, you add the line g_timers.begin("my data sort"); and at the end of where the code does its thing you add the line g_timers.pause("my data sort");. You can add as many checkpoints as you see fit for different part of the code – just make sure the string timer identifier passed to the g_timers.begin method is identical to the one passed to the corresponding g_timers.pause method.

Finally, whenever you want to see a report of the accumulated times of all the timers you’ve planted, simply call g_timers.show(). You might probably also want to call g_timers.reset() after that to zero all the accumulated time measurements.

var timers = {};
timers.new = function()
{
    var self = {}
    var t1,total;

    self.begin = function(id) {
        t1[id] = +new Date();
        if (!(id in total))
            total[id] = 0;
    }

    self.pause = function(id) {
        if (id in t1) {
            var t2 = +new Date();
            total[id] += t2-t1[id];
            t1[id] = t2;
        }
    }

    self.show = function() {
        console.log( "Total times:")
        for (id in total)
            console.log( id+": "+total[id]+" ms." );
    }

    self.reset = function() {
        t1    = {};
        total = {};
    }

    self.reset();
    return self;
}

var g_timers = timers.new();

I think IE6 does not like the above method of creating the JavaScript object – if you have problems, just use your own flavor.

JavaScript Graphic Libraries

I’ve been looking at some JavaScript libraries to be used for data visualization – there are quite a few, from impressive heavy duty data visualization frameworks like D3 (which requires modern browsers that support SVG) through powerful vector rendering libraries like RaphaĆ«l that work across almost all browsers and versions using SVG or VML for older versions of IE.

The following is a sample from yet another library (JSDraw2DX) that works on both IE and non-IE browsers. View frame source to see the simplicity with which these libraries can create useful presentations (click and drag the yellow dots).

Linux and HP printers/scanners

While attempting to scan a document with my newly purchased HP Deskjet 3070 All-in-One Printer B611b from my Ubuntu 12.04 desktop I learned three things:

  • There’s an excellent scanning software for Linux called XSane
  • Ubuntu 12.04 comes with the drivers for most HP printers already built in, so there was nothing else needed other then launcing XSane and scanning the document
  • hplipopensource.com contains the latest HP drivers for Linux just in case you don’t have the relevant driver for you printer pre-installed. The site even has a wizard which will guide you through to the driver you need to download and how to install it so your Linux can see your printer (I learned this from here)

Combination Algorithms and the Natural Logarithm Base

While comparing the efficiency of my iterative method for generating combinations vs. the recursive method shown at the Rosetta Code site I picked up on a pattern that at first glance is not obvious so I’m still looking into it. It seems that the ratio of the efficiency between the algorithms seems to approach the natural logarithm base (2.71828182…)

Here’s the code – I’ll do some more research before attempting to explain what is going on here

------------------------------------------------------------------------------
-- Lua recursive code for generating combinations from Rosetta Code
function map(f, a, ...)
    c1=c1+1
    if a then return f(a), map(f, ...) end
end

function incr(k)
    return function(a) return k > a and a or a+1 end
end

function combs1(m, n)
    if m * n == 0 then return {{}} end
    local ret, old = {}, combs1(m-1, n-1)
    for i = 1, n do
        for k, v in ipairs(old) do
            ret[#ret+1] = {i, map(incr(i), unpack(v))}
        end
    end
    return ret
end

--for k, v in ipairs(combs(3, 5)) do print(unpack(v)) end

-----------------------------------------------------------------------------
-- My Lua iterative code for generating combinations
-- input a, b (a number of slots, b number of symbols)
function combs2(a,b)
    if a==0 then return end
    local taken = {}
    local slots = {}
    for i=1,a do slots[i]=0 end
    for i=1,b do taken[i]=false end
    local index = 1
    while index > 0 do repeat

        repeat
            c2=c2+1
            slots[index] = slots[index] + 1
        until slots[index] > b or not taken[slots[index]]

        if slots[index] > b then
            slots[index] = 0
            index = index - 1
            if index > 0 then
                taken[slots[index]] = false
            end
            break
        else
            taken[slots[index]] = true
        end

        if index == a then
            -- for i=1,a do
            --     io.write( slots[i] )
            --     io.write( " " )
            -- end
            -- io.write( "n")
            taken[slots[index]] = false
            break
        end

        index = index + 1
    until true end
end

------------------------------------------------------
-- A comparison of the number of operations required
-- by recursive and non-recursive method. Tests show
-- that as the number of slots and symbols rise, the
-- ratio of the efficiency shows an interesting pattern
-- and has to do with the base of the natural logarithm
-------------------------------------------------------

function compare(sl,sy)
    c1=0
    c2=0
    combs1(sl, sy)
    combs2(sl, sy)
    print( "slots:"..sl..", symbols:"..sy.." => iterative/recursive ratio:"..c2/c1)
end

for n=2,11 do
    compare(n,n)
end

Generating permutations

For some experiment I’m conducting, I needed to generate all permutations given a number of slots and a number of symbols. I also didn’t want to use built-in libraries like Python or other languages have to generate them, so I picked Lua to avoid being tempted to do so (and because its a cool language).

Now there are Lua implementations to do this, for example on Rosetta Code but the all examples I’ve seen either use recursion or appeared oversized.

I wrote the following non-recursive function in Lua to generate permutations given as input a number of slots and a number of symbols.

-- generate permutations
-- input a, b (a number of slots, b number of symbols)
function permutations(a,b)
    if a==0 then return end
    local taken = {}
    local slots = {}
    for i=1,a do slots[i]=0 end
    for i=1,b do taken[i]=false end
    local index = 1
    while index > 0 do repeat

        repeat
            slots[index] = slots[index] + 1
        until slots[index] > b or not taken[slots[index]]

        if slots[index] > b then
            slots[index] = 0
            index = index - 1
            if index > 0 then
                taken[slots[index]] = false
            end
            break
        else
            taken[slots[index]] = true
        end

        if index == a then
            for i=1,a do
                io.write( slots[i] )
                io.write( " " )
            end
            io.write( "n")
            taken[slots[index]] = false
            break
        end

        index = index + 1
    until true end
end

The following is in generator form:

function perm( sl, sy )
    local permutations = function( a,b )
        if a==0 then return end
        local taken = {}
        local slots = {}
        for i=1,a do slots[i]=0 end
        for i=1,b do taken[i]=false end
        local index = 1
        while index > 0 do repeat
            repeat
                slots[index] = slots[index] + 1
            until slots[index] > b or not taken[slots[index]]
            if slots[index] > b then
                slots[index] = 0
                index = index - 1
                if index > 0 then
                    taken[slots[index]] = false
                end
                break
            else
                taken[slots[index]] = true
            end
            if index == a then
                coroutine.yield(slots)
                taken[slots[index]] = false
                break
            end
            index = index + 1
        until true end
    end

    return coroutine.wrap(function () permutations(sl, sy) end)
end

function test(a,b)
    ci = perm(a,b)
    a = ci()
    while a~=nil do
        for i=1,#a do
            io.write( a[i] )
            io.write( " " )
        end
        io.write( "n")
        a = ci()
    end
end

This method can probably be improved since it goes over all possible combinations (both repeating and non-repeating) and in the process filters out the repeating ones.

Future of mobile development

It took me a while to converge to a decision regarding what cross platform mobile development platform I should commit myself to (at least in the foreseeable future). After considering the various frameworks, I came to the understanding that MoSync is the winner for me. The reasons are that MoSync best matches my view on where mobile development technology should be going:

  • It provides the ability to quickly develop web or native UI for a mobile application using HTML5 and get immediate feedback on the mobile device when changing the UI code on the development platform.
  • It provides a C++ abstraction layer for running native code on all the major mobile device operating systems.
  • It provides a bridge (called wormhole) to effectively pass bidirectional events between the HTML5/JavaScript world and the C++ world.

This struck a chord with me as this was the exact philosophy I had in mind when developing JNEXT, which enables smoothly combining demanding CPU and native resource tasks with demanding UI tasks while using the appropriate technology for each.

As a note regarding the longer-term future of mobile development, my guess is that what is going to happen is that native mobile APIs (using Objective C for iPhone, Java for Android, C# for Windows Phone etc.) will become as interesting to future mobile application developers as assembly language is for todays application developers. There will be just too many interesting apps to create and distribute to be bothered with wasting time learning the specifics of any particular “CPU”.

Software reliability

Endless volumes have been written about the problems associated with the growing complexity of software. It is interesting how the ancient story about the tower of Babel seems to be materializing in the form of broken communications between software applications located on different hardware products and vendors.

I don’t claim there is a silver bullet that would solve this, but there is one thing I’m certain of, and that is that a lot of this mess has to do with doing things fast instead of doing them well, complicating instead of simplifying, building huge buildings without first taking the time to make sure they have rock-solid foundations.

This is obviously a generalization, and as such has its exceptions – but I’m referring to much of what I’ve experienced over many years in a wide spectrum of development platforms, software products, SDKs and what not.

I suspect part of the reason for what I call the “race for quantity at the expense of quality” has to do with the Western way of life and its financial driver – i.e capitalism. This philosophy tends to orient people more towards quantity than towards quality, resulting in companies wanting to churn more and more features so their products will have more check marks than their competition in the competitive analysis chart.

But this also has to do with caring enough about the people who are going to use your product. This is something that can be improved by just starting to give a damn about making your product simple and reliable to the point that there is no room for ambiguity or question as to how to make it work.

There are so many examples of this that I probably don’t have to specify – any software developer has come across them. Hours or days of research that result in an answer that had it been documented, would have put the problem to rest in 5 minutes. As another example – endless code with bad documentation that upon inspection can be reduced to code a fraction of the size and simple examples that would provide much better help than any documentation would. Sites like Stack Overflow are great, but in a way many of the questions posted there stand as evidence of this problem.

The trigger for this post is me currently wasting time trying to understand why a very simple code example does not work as advertised in a certain commercial product. I’ve been through this before and I’m experienced enough to know that it has nothing to do with the code logic. The question I’m asking myself is “why do I have to waste time on this ?”. As a consumer, you wouldn’t put up with buying a TV, turning it on, switching it to the channel you like, only to find an error message on the screen saying “Error: situation not anticipated, please configure the firmware accordingly”.

Keep it simple. Keep it reliable. Strive for better instead of larger – this might not solve the software complexity issue, but at least it won’t make it worse.

Added a new JavaScript game

Took a couple of hours to create an old game I played about 30 years ago on a friend’s digital watch… I don’t remember the model of the watch, the name of the game or even the friend’s name, but I do remember everyone took turns trying to play and beat the high score. The idea was to try and delay the stampede of marching numbers by selecting a digit and removing it from the advancing number sequence.

I’ll try and continue adding another JavaScript game once a week, at least until some submissions start to arrive.

Google can’t find everything

For some reason there’s an old song I learned at school more than 35 years ago, whose tune and lyrics I can still remember. It goes like this:

I hear echoes that are dear to me
that whisper oh so sweetly to my heart
You must come, come to those you love across the sea
nor let lt be to long afore you start

So I go on my journey through the bonnie heather
never tiring though I walk a hundred miles
There’s a song in my heart be black or bright the weather
for so much am I nearer to the isles.

Its either an old Scottish or Irish song and of course I’ve never heard it since that music lesson back in 4th grade.
I thought that a Google search would shed more light on the source of the song, but no matter which part of text I quoted the search yielded nothing of relevance. Quite interesting, considering the mind boggling volume of texts of both present and past works that are indexed by Google.Perhaps this was something the music teacher made up…

4K JavaScript games

I’ve added a new JavaScript 4K page will host JavaScript games that are contained in one html file which is no larger than 4K. This is an even more difficult constraint than the Java 4K challenge since the Java files are in compiled bytecode whereas Javascript is at most minified source code.

The gallery currently contains only one game which I wrote a few years ago during a short break on a PC with no Internet connection, development tools or ways of saving the data to external devices. I kept a printout as a souvenir which I recently found and which gave me the idea for the JavaScript4K gallery/contest. If it picks up, I’ll make it a site of its own.