0

i just wanted to know javascript number size because i want to send lot of them via network per frame and i must know a measure of how many im gonna send per second.

As i readed: According to the ECMAScript standard, there is only one number type: the double-precision 64-bit binary format IEEE 754 value (number between -(2^53 -1) and 2^53 -1).

So if im gonna send lot of diferent numbers(example later) if all numbers between -(2^53 -1) and (2^53 -1) use same memory i may just combinate them like 567832332423556 and then locally split them locally when received instead of sending a lot of diferent numbers, because anyway that unique number "567832332423556" sends same information as a separated 5,6,7,8... but in one so its supossed to waste many less if it haves same size as a single 5.

Is this true or just im so confused? pls explain me :(.

var data = Array2d(obj.size); //Size can be between 125 and 200;`                                                                                   
Array2d: function (rows) { //The number of rows and files are same
             var arr = [];
             for (var i=0;i<rows;i++) arr[i] = [];
             return arr;
         }, 

 ...                                                                                                               

if (this.calculate()) {
    data[x][y] = 1;
} else {
    data[x][y] = 0;
}

and somewhere in the code i change those 1 to any number from 2 to 5 so numbers may be from 0 to 5 depends of the situation.

Example:

[
    [0,0,2,1,3,4,5,0,2,3,4,5,4(200 numbers)], 
    [0,5,2,1,5,1,0,2,3,0,0,0,0(200 numbers)]
    ...(200 times)
]   

*And i really need All numbers, i cant miss even one.

If in therms of size 5 is shame as 34234 so i could just do something like:

[
    [0021345023454...(20 numbers 10 times)], 
    [0021345023454...(20 numbers 10 times)]
    ...(200 times)
] 

and it may use 20 times less because if 5 size is the same as 2^53 i just stack numbers 20 by 20 and they should waste lot less (ofc, 20 numbers less by stacking 20, at least in the network, maybe the local split is a little big but locally i do few things so i can handle that).

Covacs
  • 3
  • 4
  • how do you know where to split `567832332423556` to get `[5,6,7,8,3,23,324,23,5,5,6]` at the other end? ... why not send `JSON.stringify([5,6,7,8,3,23,324,23,5,5,6])` or `[5,6,7,8,3,23,324,23,5,5,6].join(',')` (saves two bytes over JSON) - these methods will be up to twice as big (in the case of single digit numbers) - but there's no ambiguity – Jaromanda X Jan 21 '17 at 02:21
  • i split it a simple way, that 324 and those 23 were writen just because its a example but in the array they gonna be all single numbers. Sorry, im gonna explain better what im gonna send to people understand me better. an double array like [[1,2,4,5,6,3,2,5,3,2,5,6,7,8],[1,2,4,5,6,3,2,5,3,2,5,6,7,8].......] so if i can put all numbers of a block in one i can just forget of two dimensional array and then do something like [12456325325678,12456325325678......] and by the way waste less bytes. And really a 120 waste same as a 2^53 -1? – Covacs Jan 21 '17 at 02:25
  • Side note: there is no direct correlation between how JavaScript stores numbers and what is sent over network. If you decide which one you are actually interested in one can provide better suggestions. Also consider to [edit] your post with data sample that is more realistically reflects your needs. – Alexei Levenkov Jan 21 '17 at 02:32
  • var data = Array2d(obj.size); //Size can be between 125 and 200; Array2d: function (rows) { var arr = []; for (var i=0;i – Covacs Jan 21 '17 at 02:37
  • my problem is the amount of data im going to send because if i have such big arrays sending them all the time i want them to have a littler size. So i supossed that if i stack those numbers like that they gonna waste less, because i can send twice less data (if 99 waste like a 5) if i send numbers 2 by 2. – Covacs Jan 21 '17 at 02:50
  • When you say numbers, do you mean integers? When you say they all gonna be "single numbers", do you mean they are all single, unsigned decimal digits? Please add more information about the range of numbers and your requirements as requested by @AlexeiLevenkov . – traktor Jan 21 '17 at 04:24

1 Answers1

0

Precise limits on numbers are covered in What is JavaScript's highest integer value that a Number can go to without losing precision? - 9007199254740991 for regualr arithmetic operations, 2^32 for bit operations.

But it sounds like you are more interested in network representation than memory usage at run-time. Below is list of options from less to more compact. Make sure to understand your performance goals before moving away from basic JSON solution -as cost and complexity of constructing data rises more compatc representation you pick.

  • Most basic solution - JSON representation of existing array gives pretty decent ~2 characters per value representation:

    [[0,1,5,0,0],[1,1,1,1,1],[0,0,0,0,0]]     
    
  • Representing all numbers in a row as one big string gives ~1 character number:

    ["01500","11111","00000"]
    
  • Representing same values as concatenated numbers does not bring much savings - "11111" as string is about as long as the same 11111 as number - you add pair of quotes per row for string but one coma pare 16 values when packing as numbers.

  • You can indeed pack values to number in more compact form since the range is 0-5 using standard 6-ary value you get ~6^20 per on JavaScript number which is not significant savings over 16 values per number which you get with just representing as digits concatenation.

  • Better packing would be to represent 2 or 3 values as one character - 2 values give 36 combinations (v1 * 6 + v2) which can be written with just [A-Z0-9], 3 - 216 value which mostly fits into regular characters range.

  • you can go strictly binary representation (3-4 bit per value) and send via WebSockets to avoid cost of converting to text with regular requests.

  • Whether you go with binary or text representation one more option is compression - basic RLE compression may be fine if your data have long sequences of same value, other compression algorithms may work better on more random data. There are libraries to perform compression in JavaScript too.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179