Git Product home page Git Product logo

tiny-binary-format's Introduction

Travis Status

tiny-binary-format

Memory efficient JS using binary formats instead of objects.

var BinaryFormat = require('tiny-binary-format');

var Tile = new BinaryFormat([
  { length: 8, name: 'type' },
  { length: 8, name: 'height' },
  { length: 1, name: 'vegetation' }
]);

Tile.pack(4, 48, 1);
// 000000100001100001 (2145)

Tile.unpack(2145);
// { type: 4, height: 48, vegetation: 1 }

Tile.unpackArray(2145);
// [4, 48, 1]

This library is designed to be used as a very thin wrapper around dealing with binary data yourself.

Define a format by creating an instance of BinaryFormat and passing in your field specifications. Then call the resulting object, passing instance data as arguments in order to create a binary representation.

When you need to read the values back, pass the number to either .unpack which returns the values in an object, or .unpackArray which returns them in an array.

Install

npm install tiny-binary-format
# or
bower install tiny-binary-format

Gotchas

The module was designed for storing lightweight tile representations for games, however it could be used for a lot of other things too. Just remember that once the data has been serialized, it will always be read back out as numbers.

This module should be used for removing the neccessity of working with bitwise operators when dealing with binary formats. It's not a framework and it won't hold your hand.

This module does no error handling whatsoever. If you care enough about performance to be using a binary format in the first place, then you'll appreciate the transparency of the pack and unpack methods.

However, this does mean that if you pass in a 9 bit number into an 8 bit field, you'll lose precision silently. Use it carefully.

As pointed out by @dioxmat, you're still bound by the restrictions of MAX_SAFE_INTEGER. If you're dealing with integers greater than 2 ^ 53, then expect things to break.

FAQ

What if I need to store strings or objects too?

If you arrived here looking for a Javascript library for parsing binary format files that include lots of data types, you've come to the wrong place. Go and check out binary-format

It doesn't seem to work for 64 bit floating numbers?

That's because the Javascript bitwise operators don't either. For a quick workaround, use the 64-bit-support branch.

What would be a good use case for this library?

This code was born from a game engine, read more about where it might be useful in the accompanying blog blost.

tiny-binary-format's People

Contributors

megawac avatar poporul avatar ziad-saab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tiny-binary-format's Issues

Test cases for `unpack` and `unpackArray` are wrong

The test inputs for unpack and unpackArray are calling these methods with a test case that is itself an array. Even though the tests are passing, the inputs to these functions should be integers and not arrays.

The only reason why the tests are passing is because the functions create their returned array / object and then add to it.

Add warnings or errors for numbers that are going to break

Because Javascript doesn't support bitwise operations on numbers > 32 bit integers, we need to provide some kind of heads up when someone creates a format with a field length that will break.

If someone tries to create a field that's not a number, is a floating point number, or is an integer bigger than we can manipulate with a bitwise operators, then throw some pretty liberal errors. As long as this stuff lives within the constructor, it should be fine.

If you can think of any other edge cases that could also be troublesome, leave a comment here or send a PR.

Unpack bug

You initialize the unpacked variable to an empty array, while it should be an empty object.

Indeed, Tile.unpack(2145); will always return an empty array.

Bower support

I know that this library was designed for game development but I can see a use for it in business application as well. I recently had a screen which had numerous check-boxes that was represented by a bitmask to tell which boxes were checked and which had not been in the URL routing. The challenge came when we had to change the feature to allow for a number per selection instead of a Boolean. I was never fully happy with our approach and I would have loved to use your library on the front end to map the different selections to a single number that can be translated back to the selections. If this was in Bower it would make it much easier to have this kind of solution in our toolbox.

Worry about performance hit on double precision float numbers arguments

I am researching bit packing technologies in javascript and I come across this module. I am making a game too so I am glad this is born for games. The problem is javascript only supports integer bitwise operation. But unlike other languages, javascript numbers are double precision float numbers. So the numbers in the arguments need to be converted to integer before bit shifting and other bit operations. Does that have any negative performance impact?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.