Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to encode/decode large quantities of data for a JavaScript client?

I'm writing a standalone javascript application with Spine, Node.js, etc.(Here is an earlier incarnation of it if you are interested). Basically, the application is an interactive 'number property' explorer. The idea being that you can select any number, and see what properties it possesses. Is it a prime, or triangular, etc? Where are other numbers that share the same properties? That kind of thing.

At the moment I can pretty easily show like numbers 1-10k, but I would like to show properties for numbers 1-million, or even better 1-billion.

I want my client to download a set of static data files, and then use them to present the information to the user. I don't want to write a server backend.

Currently I'm using JSON for the data files. For some data, I know a simple algorithm to derive the information I'm looking for on the client side, and I use that (ie, is it even?). For the harder numbers, I pre compute them, and then store the values in JSON parseable data files. I've kinda gone a little overboard with the whole thing - I implemented a pure javascript bloom filter and when that didn't scale to 1 million for primes, I tried using CONCISE bitmaps underneath (which didn't help). Eventually I realized that it doesn't matter too much how 'compressed' I get my data, if I'm representing it as JSON.

So the question is - I want to display 30 properties for each number, and I want to show a million numbers...thats like 30 million data points. I want the javascript app to download this data and present it to the user, but I don't want the user to have to download megabytes of information to use the app...

What options do I have for efficiently sending these large sets of data to my javascript only solution?

Can I convert to binary and then read binary on the client side? Examples, please!

like image 725
dsummersl Avatar asked Nov 17 '25 06:11

dsummersl


1 Answers

How about just computing these data points on the client?

You'll save yourself a lot of headache. You can pre-compute the index chart and leave the rest of the data-points to be processed only when the user selects a particular number.

For the properties exhibited per number. Pure JavaScript on modern desktops is blindingly fast (if you stay away from DOM), I think you'll find processing speed differences are negligible between the algorithmic vs pre-computed JSON solution and you'll be saving yourself a lot of pain and unnecessary bandwith usage.

As for the initial index chart, this displays only the number of properties per number and can be transferred as an array:

'[18,12,9,11,9,7,8,2,6,1,4, ...]'

or in JSON:

{"i": [18,12,9,11,9,7,8,2,6,1,4, ...]}

Note that this works the same for a logarithmic scale since either way you can only attach a value to 1 point in the screen at any one time. You just have to cater the contents of the array accordingly (by returning logarithmic values sequentially on a 1-2K sized array).

You can even use a DEFLATE algorithm to compress it further, but since you can only display a limited amount of numbers on screen (<1-2K pixels on desktop), I would recommend you create your solution around this fact, for example by checking if you can calculate 2K *30 = 60K properties on the go with minimal impact, which will probably be faster than asking the server at this point to give you some JSON.

UPDATE 10-Jan-2012

I just saw your comment about users being able to click on a particular property and get a list of numbers that display that property.

I think the intial transfer of number of properties above can be jazzed up to include all properties in the initial payload, bearing in mind that you only want to transfer the values for numbers displayed in the initial logarithmic scale you wish to display (that means that you can skip numbers if they are not going to be represented on screen when a user first loads the page or clicks on a property). Anything beyond the initial payload can be calculated on the client.

{ 
  "n": [18,12,9,11,9,7,8,2,6,1,4, ...] // number of properties x 1-2K
  "p": [1,2,3,5,7,13,...] // prime numbers x 1-2K
  "f": [1,2,6, ...] // factorials x 1-2K
}

My guess is that a JSON object like this will be around 30-60K, but you can further reduce this by removing properties whose algorithms are not recursive and letting the client calculate those locally.

If you want an alternative way to compress those arrays when you get to large numbers, you can format your array as a VECTOR instead of a list of numbers, storing differences between one number and the next, this will keep space down when you are dealing with large numbers (>1000). An example of the JSON above using vectors would be as follows:

{ 
  "n": [18,-6,-3,2,-2,-2,1,-6,4,-5,-1, ...] // vectorised no of properties x 1-2K
  "p": [1,1,2,2,2,6,...] // vectorised prime numbers x 1-2K
  "f": [1,1,4, ...] // vectorised factorials x 1-2K
}
like image 145
Steven de Salas Avatar answered Nov 19 '25 18:11

Steven de Salas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!