function transform(tree,fn){    var root = tree,        node = tree,        child,        parent,        is_node,        dir;    root.dir = 0;    while(true) {        is_node = typeof(node)===object;        dir = is_node ? node.dir : 2;        if (dir < 2)            child = node[dir],            node.dir++,            child.parent = parent = node,            child.dir = 0,            node = child;        else if ((changed = fn(node))!==undefined)            changed.parent = parent,            changed.dir = 0,            node = changed;        else            if (!parent)                return node;            else                parent[parent.dir-1] = node,                node = parent,                parent = node.parent;    };};// TESTvar tree = [[1,2],[[3,4],[5,6]]];console.log(    JSON.stringify(transform(tree,function(a){        if (a[0]===1) return [3,[5,5]];        if (a[0]===5) return 77;    })) === [[3,77],[[3,4],77]]);I need to make this function as fast as possible, no matter what. I'm aware this is far from the optimal implementation. How could I improve it? Asm.js, maybe?
JavaScript bottom-up tree transformer aimed for performance
javascript;algorithm;tree