Q:

Efficient data migration

I'm trying to make a market depth chart with data from a exchange and a Google JS chart.
The good news is I got it working, but the bad news is the code to prepare the data to be used by google charts is just bad. I know there should be far better ways to do this but I can't come to a proper solution.
The original data is as following:
{bids: [[price, amount], [price, amount], ...],
 asks: [[price, amount], [price, amount], ...]}

Google chart needs it like this:
[[price, bid, ask], [price, bid, ask], ...]

I have the following (ugly) code:
var newData = [['price', 'bid', 'ask']];
var t = 0;
var tt = [];
var p = [];
for (var i = data.bids.length -1; i > -1; i--) {
    t += data.bids[i][1];
    tt.push(t);
    p.push(data.bids[i][0])
}
var ii = 0;
for (var i = tt.length - 1; i >= 0; i--) {
    newdData.push([p[ii], tt[i],null])
    ii++;
};
t = 0;
for (var i = data.asks.length -1; i > -1; i--) {
    t += data.asks[i][1];
    newData.push([data.asks[i][0], null, t]);
}

If I create a google chart with the newData it works as expected. How can I make the code better or more efficient?

A:

I took some time to look at your code and rewrite it a little bit.
My first advice is, try to make your code a little bit more verbose. It will be only written once and read over and over. It's very important that the code communicates what it's supposed to do, either through variable naming or comments.
The Art Of Readable Code is a great book on this topic, thoroughly covered by "Implementation Patterns" as well.
So...you can try this out:
function newAlgorithm(data){
    // Intial row needs data for headers
    var newData = [['price', 'bid', 'ask']],
    // Optimizes for loops
    entriesCount = data.bids.length,
    // Will be calculcated later on
    bidAmountSum = 0,
    askAmountSum = 0,
    // Will contain all entries that should be pushed into the final array
    bidEntries = [],
    askEntries = [];

    // Calculate bid and ask sums
    for(var i = 0; i < entriesCount; i++){
        bidAmountSum += data.bids[i][1];
        askAmountSum += data.asks[i][1];
    }

    // Create bid and ask entries for each one in one pass, since their count is the same
    for(var i = 0; i < entriesCount; i++){
        // Pid price is iterated in reverse
        // Bid amount sum is cumulative sum of previous bids for each point in iteration
        bidEntries.push([data.bids[data.bids.length-1 - i][0], bidAmountSum, null]);
        bidAmountSum -= data.bids[i][1];

        // Price is iterated in forward order
        // Ask amount sum is cumulative sum of previous asks for each point in iteration
        askEntries.push([data.asks[i][0], null, askAmountSum]);
        askAmountSum -= data.asks[i][1];
    }

    return newData.concat(bidEntries).concat(askEntries.reverse());
}

I tested how it performs when tested with NodeJS runtime. I increased the dataset from 100 entries through 2,000,000.

I also made in-browser test here: http://jsperf.com/chart-data-repacking
I hope this helps.

