1.4.9 • Published 6 months ago

fast-boolean-array v1.4.9

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Fast Boolean Array Build

In JavaScript, when working with large arrays of boolean values, a common challenge is efficiently indexing and retrieving these values. Using a regular JavaScript array to store booleans is straightforward, but it is memory-inefficient. While booleans are conceptually 1-bit values, JavaScript engines, like V8 (in Chrome and Node.js), allocate 1 byte (8 bits) per boolean for optimization purposes. This can waste a significant amount of memory when dealing with large arrays.

Fast Boolean Array solves this issue by utilizing bit manipulation to store booleans in a compact format. It uses a Uint8Array and stores each boolean as a single bit within the 8 bits of each byte. This allows you to index and retrieve boolean values by integers (e.g., the 0th boolean, 1st boolean, etc.) while only using a fraction of the memory.

For detailed benchmark results, see below.

Features

  • Memory Efficiency: Stores booleans in bits rather than bytes, drastically reducing memory usage.
  • Fast Set Performance: Up to 10x faster sets for fast data re-mapping.
  • Familiar API: Map and Set like API for minimal learning curve. Intuitive helper functions, and works in for.. of.
  • Array like interface: call .accessLikeArray() to access it like an arraysomeArray[index] (beware of the performance penalty caused by the Proxy)!

Usage example:

Here's how to use the Fast Boolean Array:

import BooleanArray from "fast-boolean-array";
// const BooleanArray = require('fast-boolean-array'); commonjs works too.

// Create a new BooleanArray with the desired length
const booleans = new BooleanArray(2);

// Set a value at a specific index
booleans.set(0, true);
booleans.set(1, false);

// Retrieve a value at a specific index
console.log(booleans.get(0)); // Output: true
console.log(booleans.get(1)); // Output: false

booleans.set(3, true); // will not throw, but returns false as the boolean is not found
console.log(booleans.get(3)); // Output: false

booleans.setSafe(3, true); // will throw RangeError because the array has a length of 2

Installation

Install the package:

npm install fast-boolean-array
pnpm install fast-boolean-array

API

new BooleanArray(size)

Creates a new boolean array of the given size. All values are initialized to false.

  • Parameters:
    • size (number): The number of booleans the array should hold.

set(index, value)

Sets the boolean value at the specified index.

  • Parameters:
    • index (number): The position in the array to set the value.
    • value (boolean): The value to set (true or false).

get(index)

Gets the boolean value at the specified index.

  • Parameters:

    • index (number): The position in the array to retrieve the value.
  • Returns:

    • boolean: The value at the given index.

Performance Breakdown

Our benchmark compares the performance of our Fast Boolean Array library against Vanilla JavaScript arrays in terms of get and set operations across varying numbers of Booleans. The results below reflect an updated benchmark algorithm that performs 1,000 runs for each x amount of Booleans to better simulate real-world scenarios.

Performance Breakdown

1 Boolean

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array0.0023600081
Set Fast Boolean Array0.0042300061
Get Vanilla Array0.00053000N/A1
Get Fast Boolean Array0.00291000N/A1

As useless as having just 1 boolean stored in an array might be, For 1 boolean, the Fast Boolean Array is 1.8x slower for Set and 5.5x slower for Get operations. Memory usage is nearly identical, with a small advantage for the Fast Boolean Array in terms of bytes.


100 Booleans

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array0.00332000107100
Set Fast Boolean Array0.0082400018100
Get Vanilla Array0.00149000N/A100
Get Fast Boolean Array0.00581000N/A100

Observation: For 100 booleans, Fast Boolean Array is 2.5x slower in Set operations but 3.9x more memory efficient. However, Fast Boolean Array's' Get operation is 3.9x slower . compared to the Vanilla Array.


1,000 Booleans

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array0.0959900010091000
Set Fast Boolean Array0.044670001301000
Get Vanilla Array0.01261000N/A1000
Get Fast Boolean Array0.05476000N/A1000

For 1,000 booleans, Fast Boolean Array is 2.1x faster for Set operations and uses 7.7x less memory. However, Fast Boolean Array's' Get operation is 4.3x slower . compared to the Vanilla Array.


10,000 Booleans

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array0.300750001000910000
Set Fast Boolean Array0.04225000125610000
Get Vanilla Array0.06054000N/A10000
Get Fast Boolean Array0.12325000N/A10000

Observation: For 10,000 booleans, Fast Boolean Array is 7.1x faster in Set operations and uses 8x less memory. However, Fast Boolean Array's' Get operation is 2x slower . compared to the Vanilla Array.


100,000 Booleans

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array1.67949000100011100000
Set Fast Boolean Array0.1540000012506100000
Get Vanilla Array0.04920000N/A100000
Get Fast Boolean Array0.07523000N/A100000

For 100,000 booleans, Fast Boolean Array is 10.9x faster in Set operations and uses 8x less memory. However, Fast Boolean Array's' Get operation is 1.5x slower .


1,000,000 Booleans

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array20.0788900010000111000000
Set Fast Boolean Array3.428170001250071000000
Get Vanilla Array0.49531000N/A1000000
Get Fast Boolean Array0.74617000N/A1000000

For 1,000,000 booleans, the Fast Boolean Array is 5.9x faster for Set operations and uses 8x less memory. However, Fast Boolean Array's' Get operation is 1.5x slower .


10,000,000 Booleans

TestTime (ms)Memory (Bytes)Booleans
Set Vanilla Array266.657780001000001310000000
Set Fast Boolean Array35.53492000125000710000000
Get Vanilla Array4.90792000N/A10000000
Get Fast Boolean Array7.57038000N/A10000000

For 10,000,000 booleans, Fast Boolean Array is 7.5x faster for Set operations and uses 8x less memory. However, Fast Boolean Array's' Get operation is 1.5x slower .


Summary

  • The Fast Boolean Array is significantly more efficient in terms of memory usage and Set operation speeds, particularly as the number of booleans increases.
    • For example, as stated previously, at 10,000,000 booleans, it is 7.5x faster in Set operations and uses 8x less memory.
  • The Get operation, however, tends to be slower on a Fast Boolean Array, with it being up to 1.5x slower compared to the regular vanilla boolean[] in larger datasets.

If memory efficiency and Set performance are priorities, the Fast Boolean Array is clearly advantageous, but if Get performance is more important, the Vanilla Array may still offer better results for certain use cases.

We are still working on improving the performance of the toArray function, but you can already use it today to convert back to a vanilla array, though generating one from scratch is currently more effecient.


Contributing

Contributions are welcome! If you'd like to report a bug, suggest a feature, or submit a pull request, please visit the GitHub repository.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgements

Thank you to the open-source community for inspiration and support!

1.4.9

6 months ago

1.4.8

6 months ago

1.4.7

6 months ago

1.4.6

7 months ago

1.4.5

7 months ago

1.4.4

7 months ago

1.4.3

7 months ago

1.4.2

7 months ago

1.4.1

7 months ago

1.4.0

7 months ago

1.3.4

7 months ago

1.3.3

7 months ago

1.3.2

7 months ago

1.3.1

7 months ago

1.2.1

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago