New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@bitarray/es6

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bitarray/es6

An ES6 BitArray class for easy and native-like operations on sequences of bits

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
11
175%
Maintainers
1
Weekly downloads
 
Created
Source

@bitarray/es6

A BitArray class for easy operations on sequences of bits.

Rationale

The library implements ArrayBuffers behind the hood. This allows for:

  • optimal memory usage: each bit is effectively coded in just one bit in memory, as opposed to being coded as, e.g. booleans or integers in regular arrays.

  • efficient bitwise operations: operations treat 32 bits at once, as opposed to dealing with entries one by one in regular arrays.

It comes at the cost that accessing a single bit at a given index will be slightly more expensive than accessing the value stored in a regular array: indeed, the interesting bit needs to be extracted once a Uint32 value has been found first.

So do use this library if you need to apply operations on bunches of bits and/or if memory usage matters to you. But don't use this library if you rather need to heavily manipulate bits one at a time.

Having said that, in the vast majority of cases, any difference either way should be mostly unnoticeable; and you may very well opt to use this library for the sole reason that it provides a convenient interface for your needs.

Compatibility

The library relies on the Proxy object, which is an ES6 (aka ES2015) feature. It can NOT be polyfilled (to the extent it is used by the library).

Usage

The BitArray class extends @bitarray/typedarray. So it inherits the behaviour of standard type arrays.

It adds bit-specific methods on top.

Instantiating

import BitArray from "path/to/bitarray.js"

const bits = BitArray.from( "11001010" );

bits.count; // === 4; number of bits set to 1.

const otherbits = new BitArray(12); // 12 bits, the values of which default to zero.

// same as bits.and(otherbits)
( bits )['&']( otherbits ); // an instance holding a sequence of eight zeros

// same as bits.or(otherbits)
( bits )['|']( otherbits ); // an instance holding the same sequence as bits

// same as bits.xor(otherbits)
( bits )['^']( otherbits ); // an instance holding the same sequence as bits

Iterating

for( let i=0; i<bits.length; i++ )
  // do something with bits[i]

bits.forEach( (val, i, arr) => { /* do something */ } );

for( let i in bits )
  // do something with bits[i]

for( let bit of bits )
  // do something with bit

Indexes & values

Object.keys( bits );    // [ 0,1,2,3,4,5,6,7 ]
Object.values( bits );  // [ 1,1,0,0,1,0,1,0 ]
Object.entries( bits ); // [ ["0",1], ["1",1], ["2",0], ["3",0],...]

Keywords

bitarray

FAQs

Package last updated on 03 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts