• Stars
    star
    141
  • Rank 255,000 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Memory efficient binary formats instead of objects.

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.

More Repositories

1

norman-the-necromancer

💀 Entry for JS13k 2022
TypeScript
189
star
2

ljsp

🦕 Lisp in your language.
JavaScript
84
star
3

wireworld

🔋 ClojureScript Wireworld Simulator
Clojure
66
star
4

zaphod

🌌 Clojure's API for JavaScript's Objects
JavaScript
55
star
5

vsnetrw

🗂️ A split file explorer for vscode
JavaScript
22
star
6

ng-picky

Pure Angular Color Picker.
JavaScript
12
star
7

no-mutation-cheatsheet

A quick reference for alternatives to mutable methods for Javascript collections.
10
star
8

restack

🥞 Prototype for a concatenative programming language
JavaScript
6
star
9

moriscript

Toy language built with Babel
JavaScript
5
star
10

academic-pandoc

Framework for Academic Writing with Pandoc + Markdown
Shell
5
star
11

0x55

⬛️ Image editor for 5x5 binary sprites
JavaScript
3
star
12

zigrl

🧌 A Zig + WebAssembly roguelike.
Zig
3
star
13

zhttp

🛡️ Typesafe HTTP endpoints with Express, Zod, and Static Path
TypeScript
3
star
14

arda

🗺️ Tokien inspired fantasy map generator
JavaScript
2
star
15

silmarils

👑 Utilities for building 2D games
TypeScript
2
star
16

telerin

📺 Terminal & tileset renderer for web based games
TypeScript
2
star
17

quip

Open source implementation of Quiplash
JavaScript
2
star
18

hyperdrive

Just-for-fun tiny frontend framework
JavaScript
2
star
19

dotfiles

⚙️ All things configuration
Vim Script
2
star
20

games

🎮 A personal library for building tiny 2D games.
TypeScript
2
star
21

tally

🏆 No nonsense score taking app
JavaScript
2
star
22

wings

Making a self hosted programming language on a flight
JavaScript
2
star
23

learn-react

A series of lessons that help beginners understand the basics of React.
CSS
2
star
24

midas

🫅 Traditional roguelike where everything you touch turns to gold.
JavaScript
2
star
25

vimsaurus

Vim plugin counterpart to Bashsaurus
Vim Script
2
star
26

melange

Continued with simpler architecture as https://github.com/danprince/sietch
Go
2
star
27

immutable-redux-todo

Sitepoint Tutorial on Redux & ImmutableJS
JavaScript
2
star
28

dan-and-ffion-tracker

JavaScript
1
star
29

bashsaurus

Simple API driven Thesaurus in Bash.
Shell
1
star
30

webgl-mandelbrot

🌀 WebGL Mandelbrot Fractal Viewer
JavaScript
1
star
31

node-json-canvas

Describe a node canvas image with JSON.
JavaScript
1
star
32

zig-wasm-life

Zig + WebAssembly implementation of Life
Zig
1
star
33

react-redux-simple

A selector friendly shortcut for React-Redux
JavaScript
1
star
34

seven-more-languages

Lua, Elixir, Elm, Factor, Idris, Julia, MiniKanren
Lua
1
star
35

aseprite-typescript-sprites

🌃 Export Aseprite Slices to Type Safe Code
Lua
1
star
36

typeshape

Runtime type checking against composable schemas
JavaScript
1
star
37

node-code-reload

♻️ Tests for runtime reloading strategies in node
JavaScript
1
star
38

moonlike

Experimental roguelike with moonscript and love2d
MoonScript
1
star
39

elrond

🌟 Prototype for web based fantasy console
TypeScript
1
star
40

writing-tools

✍️ Chrome extension for analysing writing.
JavaScript
1
star
41

improvise

Generative text from a recursive grammar.
JavaScript
1
star
42

sietch

🏜️ Markdown static site generator that can render and bundle interactive TypeScript components at build time
Go
1
star
43

siberia

🎨 Web based ASCII editor
JavaScript
1
star
44

clef

🎼 A tiny game that helps you learn staff notation
HTML
1
star