HTML and CSS Reference
In-Depth Information
All numeric types in JSON are stored as 64 bit floating-point numbers. That is fantastic for very large or
small numbers in scientific calculations but is overkill for most games. For an unsigned integer, that's a range of
0-18,446,744,073,709,551,615, far too high for most game scores and other numbers, which will be 32 bit or less. So,
why use twice the precision that you need?
BSON reduces the numeric overhead by having multiple sizes that it can store. Table 11-1 shows BSON's
numeric types.
Table 11-1. Chart Adapted from bsonspec.org/#/specification
Type
Bytes
Range (Signed and Unsigned)
Byte
1B (8 bits)
128-127
0-255
Int32
4B (32 bit)
−2,147,483,648-2,147,483,647
0-4,294,967,295
Int64
8B (64 bit signed int)
−9,223,372,036,854,775,808-9,223,372,036,854,775,807
0-18,446,744,073,709,551,615
Double
8B (64 bit IEEE floating point)
4.9E-324-1.7976931348623157E308
If you were developing a side-scrolling game, the number types (and their probable data types) of concern
are as follows:
1.
Location of the player in the world (two int32s)
2.
Location of enemies and obstacles in the world (two int32s per object)
3.
Current level (byte)
4.
Remaining time (int32)
5.
Player's score (int32)
Let's assume there's one enemy in the world, with the aforementioned objects. Using JSON, you'd be transmitting
56B of numeric data per update; using BSON, the total would be just 25B. That's a savings of 55 percent per update.
JSON is supported in every major JavaScript implementation. BSON doesn't lag far behind, with an official
JavaScript driver, along with several others for popular languages maintained by MongoDB and third-party drivers.
A full listing of implementations can be found at http://bsonspec.org/#/implementation . The JavaScript
implementation used for the examples is located at https://github.com/marcello3d/node-buffalo .
MessagePack
MessagePack expands on the number packing of BSON by providing five integer types, storing 1B, 2B, 3B, 5B, or 9B,
respectively, and two floating-point types, storing 5B or 9B each. Using the same parameters as the BSON example,
MessagePack further reduces the amount of data required to represent the numbers to 21B ; this is a 16 percent
improvement over BSON and a 63 percent savings versus JSON:
http://wiki.msgpack.org/display/MSGPACK/Format+specification#Formatspecificatio
n-Integers
http://msgpack.org
 
 
Search WWH ::




Custom Search