fast-boolean-array 1.1.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Fast Boolean Array
2
2
 
3
- In JavaScript, you sometimes need an array of booleans for a specific size. Using a regular JavaScript array works, but it can waste over 8x the memory. This is because JavaScript engines, like V8 (in Chrome and Node.js), represent all primitive types in a uniform way for optimization reasons. While a boolean is conceptually a 1-bit value, JavaScript engines typically allocate 1 byte (8 bits) per boolean value in memory. **Fast Boolean Array** solves this inefficiency by leveraging bit manipulation to store booleans in a compact format. It does bit manipulation on a uInt8Array, storing every boolean as a specific bit on each 8 bit int, fast and efficiently.
3
+ 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.
4
4
 
5
- View detailed benchmark results below.
5
+ 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. Each bit represents a boolean, and you can quickly access or modify a specific boolean using bitwise operations, making it both fast and memory-efficient.
6
+
7
+ For detailed benchmark results, see below.
6
8
 
7
9
  ## Features
8
10
 
@@ -15,6 +17,7 @@ View detailed benchmark results below.
15
17
  ## Installation
16
18
 
17
19
  Install the package via npm:
20
+
18
21
  ```bash
19
22
  npm install fast-boolean-array
20
23
  ```
@@ -26,21 +29,21 @@ npm install fast-boolean-array
26
29
  Here's how to use the Fast Boolean Array:
27
30
 
28
31
  ```javascript
29
- const BooleanArray = require('fast-boolean-array');
32
+ import BooleanArray from 'fast-boolean-array';
33
+ // const BooleanArray = require('fast-boolean-array'); works too
30
34
 
31
35
  // Create a new BooleanArray with the desired size
32
- const booleans = new BooleanArray(10);
36
+ const booleans = new BooleanArray(2);
33
37
 
34
38
  // Set a value at a specific index
35
39
  booleans.set(0, true);
36
-
37
- // Get the value at a specific index
38
- console.log(booleans.get(0)); // Output: true
39
-
40
- // Set another value
41
40
  booleans.set(1, false);
42
41
 
43
42
  // Retrieve it
43
+ console.log(booleans.get(0)); // Output: true
44
+ console.log(booleans.get(1)); // Output: false
45
+
46
+ booleans.set(3, false); // will throw as the array is only 3 in size
44
47
  console.log(booleans.get(1)); // Output: false
45
48
  ```
46
49
 
@@ -49,12 +52,14 @@ console.log(booleans.get(1)); // Output: false
49
52
  ## API
50
53
 
51
54
  ### `new BooleanArray(size)`
55
+
52
56
  Creates a new boolean array of the given size. All values are initialized to `false`.
53
57
 
54
58
  - **Parameters:**
55
59
  - `size` (number): The number of booleans the array should hold.
56
60
 
57
61
  ### `set(index, value)`
62
+
58
63
  Sets the boolean value at the specified index.
59
64
 
60
65
  - **Parameters:**
@@ -62,9 +67,11 @@ Sets the boolean value at the specified index.
62
67
  - `value` (boolean): The value to set (`true` or `false`).
63
68
 
64
69
  ### `get(index)`
70
+
65
71
  Gets the boolean value at the specified index.
66
72
 
67
73
  - **Parameters:**
74
+
68
75
  - `index` (number): The position in the array to retrieve the value.
69
76
 
70
77
  - **Returns:**
@@ -87,76 +94,84 @@ Our benchmark compares the performance of our Fast Boolean Array library against
87
94
  ## Performance Breakdown
88
95
 
89
96
  ### 1 Boolean
90
- | Test | Time (ms) | Memory (MB) |
91
- |--------------------------|-----------|-------------|
92
- | Set Vanilla Array | 0.00 | 0.00 |
93
- | Set Fast Boolean Array | 0.00 | 0.00 |
94
- | Get Vanilla Array | 0.00 | N/A |
95
- | Get Fast Boolean Array | 0.00 | N/A |
97
+
98
+ | Test | Time (ms) | Memory (MB) |
99
+ | ---------------------- | --------- | ----------- |
100
+ | Set Vanilla Array | 0.00 | 0.00 |
101
+ | Set Fast Boolean Array | 0.00 | 0.00 |
102
+ | Get Vanilla Array | 0.00 | N/A |
103
+ | Get Fast Boolean Array | 0.00 | N/A |
96
104
 
97
105
  **Observation:** At 1 boolean, both arrays remain equal in terms of performance and memory usage. Differences in performance are effectively immessurable, and one can assume practically identical.
98
106
 
99
107
  ### 100 Booleans
100
- | Test | Time (ms) | Memory (MB) |
101
- |--------------------------|-----------|-------------|
102
- | Set Vanilla Array | 0.00 | 0.00 |
103
- | Set Fast Boolean Array | 0.00 | 0.00 |
104
- | Get Vanilla Array | 0.00 | N/A |
105
- | Get Fast Boolean Array | 0.00 | N/A |
108
+
109
+ | Test | Time (ms) | Memory (MB) |
110
+ | ---------------------- | --------- | ----------- |
111
+ | Set Vanilla Array | 0.00 | 0.00 |
112
+ | Set Fast Boolean Array | 0.00 | 0.00 |
113
+ | Get Vanilla Array | 0.00 | N/A |
114
+ | Get Fast Boolean Array | 0.00 | N/A |
106
115
 
107
116
  **Observation:** At 100 booleans, both arrays remain equal in terms of performance and memory usage. Differences in performance are effectively immessurable, and one can assume practically identical.
108
117
 
109
118
  ### 1,000 Booleans
110
- | Test | Time (ms) | Memory (MB) |
111
- |--------------------------|-----------|-------------|
112
- | Set Vanilla Array | 0.01 | 0.00 |
113
- | Set Fast Boolean Array | 0.00 | 0.00 |
114
- | Get Vanilla Array | 0.00 | N/A |
115
- | Get Fast Boolean Array | 0.00 | N/A |
119
+
120
+ | Test | Time (ms) | Memory (MB) |
121
+ | ---------------------- | --------- | ----------- |
122
+ | Set Vanilla Array | 0.01 | 0.00 |
123
+ | Set Fast Boolean Array | 0.00 | 0.00 |
124
+ | Get Vanilla Array | 0.00 | N/A |
125
+ | Get Fast Boolean Array | 0.00 | N/A |
116
126
 
117
127
  **Observation:** At 1,000 booleans, the Fast Boolean Array is **10x** faster in set operations and uses **8x** less memory as expected. We have not yet been able to messure in actual bytes.
118
128
 
119
129
  ### 10,000 Booleans
120
- | Test | Time (ms) | Memory (MB) |
121
- |--------------------------|-----------|-------------|
122
- | Set Vanilla Array | 0.06 | 0.01 |
123
- | Set Fast Boolean Array | 0.02 | 0.00 |
124
- | Get Vanilla Array | 0.01 | N/A |
125
- | Get Fast Boolean Array | 0.01 | N/A |
130
+
131
+ | Test | Time (ms) | Memory (MB) |
132
+ | ---------------------- | --------- | ----------- |
133
+ | Set Vanilla Array | 0.06 | 0.01 |
134
+ | Set Fast Boolean Array | 0.02 | 0.00 |
135
+ | Get Vanilla Array | 0.01 | N/A |
136
+ | Get Fast Boolean Array | 0.01 | N/A |
126
137
 
127
138
  **Observation:** For 10,000 booleans, Fast Boolean Array is **3x** faster in set operations and uses **8x** less memory as expected.
128
139
 
129
140
  ### 100,000 Booleans
130
- | Test | Time (ms) | Memory (MB) |
131
- |--------------------------|-----------|-------------|
132
- | Set Vanilla Array | 1.37 | 0.10 |
133
- | Set Fast Boolean Array | 0.21 | 0.01 |
134
- | Get Vanilla Array | 0.05 | N/A |
135
- | Get Fast Boolean Array | 0.07 | N/A |
141
+
142
+ | Test | Time (ms) | Memory (MB) |
143
+ | ---------------------- | --------- | ----------- |
144
+ | Set Vanilla Array | 1.37 | 0.10 |
145
+ | Set Fast Boolean Array | 0.21 | 0.01 |
146
+ | Get Vanilla Array | 0.05 | N/A |
147
+ | Get Fast Boolean Array | 0.07 | N/A |
136
148
 
137
149
  **Observation:** At 100,000 booleans, Fast Boolean Array is approximately **6.5x** faster in set operations and uses **8x** less memory as expected.
138
150
 
139
151
  ### 1,000,000 Booleans
140
- | Test | Time (ms) | Memory (MB) |
141
- |--------------------------|-----------|-------------|
142
- | Set Vanilla Array | 19.62 | 0.95 |
143
- | Set Fast Boolean Array | 2.02 | 0.12 |
144
- | Get Vanilla Array | 0.48 | N/A |
145
- | Get Fast Boolean Array | 0.71 | N/A |
152
+
153
+ | Test | Time (ms) | Memory (MB) |
154
+ | ---------------------- | --------- | ----------- |
155
+ | Set Vanilla Array | 19.62 | 0.95 |
156
+ | Set Fast Boolean Array | 2.02 | 0.12 |
157
+ | Get Vanilla Array | 0.48 | N/A |
158
+ | Get Fast Boolean Array | 0.71 | N/A |
146
159
 
147
160
  **Observation:** For 1,000,000 booleans, Fast Boolean Array is nearly **10x** faster in set operations and uses **8x** less memory as expected.
148
161
 
149
162
  ### 10,000,000 Booleans
150
- | Test | Time (ms) | Memory (MB) |
151
- |--------------------------|-----------|-------------|
152
- | Set Vanilla Array | 259.10 | 9.54 |
153
- | Set Fast Boolean Array | 21.57 | 1.19 |
154
- | Get Vanilla Array | 4.90 | N/A |
155
- | Get Fast Boolean Array | 7.21 | N/A |
163
+
164
+ | Test | Time (ms) | Memory (MB) |
165
+ | ---------------------- | --------- | ----------- |
166
+ | Set Vanilla Array | 259.10 | 9.54 |
167
+ | Set Fast Boolean Array | 21.57 | 1.19 |
168
+ | Get Vanilla Array | 4.90 | N/A |
169
+ | Get Fast Boolean Array | 7.21 | N/A |
156
170
 
157
171
  **Observation:** At 10,000,000 booleans, Fast Boolean Array is about **12x** faster in set operations and uses nearly **8x** less memory as expected compared to Vanilla.
158
172
 
159
173
  ## Summary
174
+
160
175
  This updated benchmark confirms that the Fast Boolean Array library excels in scenarios involving large datasets, offering substantial improvements in set operation performance and memory efficiency. The sweet spot remains around 100,000 indexes, where the library achieves its most pronounced gains (6.5x faster and much lower memory usage) over Vanilla JavaScript arrays.
161
176
 
162
177
  ---
@@ -176,4 +191,3 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
176
191
  ## Acknowledgements
177
192
 
178
193
  Thank you to the open-source community for inspiration and support!
179
-
package/dist/index.cjs CHANGED
@@ -30,20 +30,59 @@ var FastBooleanArray = class {
30
30
  this.size = size;
31
31
  this.buffer = new Uint8Array(Math.ceil(size / 8));
32
32
  }
33
- set(input, value) {
33
+ /**
34
+ * Sets a boolean value at the specified index.
35
+ * @param {number} index - The index to set the boolean value at.
36
+ * @param {number} value - The boolean value to set the `index`.
37
+ * @returns {boolean} The boolean value that was set.
38
+ */
39
+ set(index, value) {
34
40
  if (value) {
35
- this.buffer[input >> 3] |= 1 << (input & 7);
41
+ this.buffer[index >> 3] |= 1 << (index & 7);
42
+ return true;
36
43
  } else {
37
- this.buffer[input >> 3] &= ~(1 << (input & 7));
44
+ this.buffer[index >> 3] &= ~(1 << (index & 7));
45
+ return false;
38
46
  }
39
47
  }
40
- get(input) {
41
- return (this.buffer[input >> 3] & 1 << input % 8) !== 0;
48
+ /**
49
+ * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
50
+ * @param {number} index - The index to set the boolean value at.
51
+ * @param {number} value - The boolean value to set the `index`.
52
+ * @returns {boolean} The boolean value that was set.
53
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
54
+ */
55
+ setSafe(index, value) {
56
+ if (index < 0 || index >= this.size) {
57
+ throw new RangeError("Index out of bounds");
58
+ }
59
+ return this.set(index, value);
60
+ }
61
+ /**
62
+ * Gets a boolean value at the specified index.
63
+ * @param {number} index - The index to get the boolean value of.
64
+ * @returns {boolean} The boolean value that was set.
65
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
66
+ */
67
+ get(index) {
68
+ return (this.buffer[index >> 3] & 1 << index % 8) !== 0;
69
+ }
70
+ /**
71
+ * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
72
+ * @param {number} index - The index to get the boolean value of.
73
+ * @returns {boolean} The boolean value that was set.
74
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
75
+ */
76
+ getSafe(index) {
77
+ if (index < 0 || index >= this.size) {
78
+ throw new RangeError("Index out of bounds");
79
+ }
80
+ return this.get(index);
42
81
  }
43
82
  get length() {
44
83
  return this.size;
45
84
  }
46
- set length(value) {
85
+ set length(_value) {
47
86
  throw new Error("Setting the length on BooleanArray's is not supported");
48
87
  }
49
88
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export default class FastBooleanArray {\r\n\tpublic size: number;\r\n\tprivate buffer: Uint8Array;\r\n\r\n\tconstructor(size: number) {\r\n\t\tthis.size = size;\r\n\t\tthis.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n\t}\r\n\r\n\tset(input: number, value: boolean) {\r\n\t\tif (value) {\r\n\t\t\tthis.buffer[input >> 3] |= 1 << (input & 7); // Set bit\r\n\t\t} else {\r\n\t\t\tthis.buffer[input >> 3] &= ~(1 << (input & 7)); // Clear bit\r\n\t\t}\r\n\t}\r\n\r\n\tget(input: number) {\r\n\t\treturn (this.buffer[input >> 3] & (1 << input % 8)) !== 0; // Check bit\r\n\t}\r\n\r\n\tget length() {\r\n\t\treturn this.size;\r\n\t}\r\n\r\n\tset length(value: number) {\r\n\t\tthrow new Error(\"Setting the length on BooleanArray's is not supported\");\r\n\t}\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAqB,mBAArB,MAAsC;AAAA,EAC9B;AAAA,EACC;AAAA,EAER,YAAY,MAAc;AACzB,SAAK,OAAO;AACZ,SAAK,SAAS,IAAI,WAAW,KAAK,KAAK,OAAO,CAAC,CAAC;AAAA,EACjD;AAAA,EAEA,IAAI,OAAe,OAAgB;AAClC,QAAI,OAAO;AACV,WAAK,OAAO,SAAS,CAAC,KAAK,MAAM,QAAQ;AAAA,IAC1C,OAAO;AACN,WAAK,OAAO,SAAS,CAAC,KAAK,EAAE,MAAM,QAAQ;AAAA,IAC5C;AAAA,EACD;AAAA,EAEA,IAAI,OAAe;AAClB,YAAQ,KAAK,OAAO,SAAS,CAAC,IAAK,KAAK,QAAQ,OAAQ;AAAA,EACzD;AAAA,EAEA,IAAI,SAAS;AACZ,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,IAAI,OAAO,OAAe;AACzB,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACxE;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export default class FastBooleanArray {\r\n\tpublic size: number;\r\n\tprivate buffer: Uint8Array;\r\n\r\n\tconstructor(size: number) {\r\n\t\tthis.size = size;\r\n\t\tthis.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n\t}\r\n\r\n\t/**\r\n\t * Sets a boolean value at the specified index.\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t */\r\n\tset(index: number, value: any) {\r\n\t\tif (value) {\r\n\t\t\tthis.buffer[index >> 3] |= 1 << (index & 7); // Set bit\r\n\t\t\treturn true;\r\n\t\t} else {\r\n\t\t\tthis.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tsetSafe(index: number, value: any) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.set(index, value);\r\n\t}\r\n\r\n\t/**\r\n\t * Gets a boolean value at the specified index.\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tget(index: number) {\r\n\t\treturn (this.buffer[index >> 3] & (1 << index % 8)) !== 0; // Check bit\r\n\t}\r\n\r\n\t/**\r\n\t * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tgetSafe(index: number) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.get(index);\r\n\t}\r\n\r\n\tget length() {\r\n\t\treturn this.size;\r\n\t}\r\n\r\n\tset length(_value: number) {\r\n\t\tthrow new Error(\"Setting the length on BooleanArray's is not supported\");\r\n\t}\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAqB,mBAArB,MAAsC;AAAA,EAC9B;AAAA,EACC;AAAA,EAER,YAAY,MAAc;AACzB,SAAK,OAAO;AACZ,SAAK,SAAS,IAAI,WAAW,KAAK,KAAK,OAAO,CAAC,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,OAAe,OAAY;AAC9B,QAAI,OAAO;AACV,WAAK,OAAO,SAAS,CAAC,KAAK,MAAM,QAAQ;AACzC,aAAO;AAAA,IACR,OAAO;AACN,WAAK,OAAO,SAAS,CAAC,KAAK,EAAE,MAAM,QAAQ;AAC3C,aAAO;AAAA,IACR;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,QAAQ,OAAe,OAAY;AAClC,QAAI,QAAQ,KAAK,SAAS,KAAK,MAAM;AACpC,YAAM,IAAI,WAAW,qBAAqB;AAAA,IAC3C;AACA,WAAO,KAAK,IAAI,OAAO,KAAK;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,OAAe;AAClB,YAAQ,KAAK,OAAO,SAAS,CAAC,IAAK,KAAK,QAAQ,OAAQ;AAAA,EACzD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAQ,OAAe;AACtB,QAAI,QAAQ,KAAK,SAAS,KAAK,MAAM;AACpC,YAAM,IAAI,WAAW,qBAAqB;AAAA,IAC3C;AACA,WAAO,KAAK,IAAI,KAAK;AAAA,EACtB;AAAA,EAEA,IAAI,SAAS;AACZ,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,IAAI,OAAO,QAAgB;AAC1B,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACxE;AACD;","names":[]}
package/dist/index.d.cts CHANGED
@@ -2,10 +2,37 @@ declare class FastBooleanArray {
2
2
  size: number;
3
3
  private buffer;
4
4
  constructor(size: number);
5
- set(input: number, value: boolean): void;
6
- get(input: number): boolean;
5
+ /**
6
+ * Sets a boolean value at the specified index.
7
+ * @param {number} index - The index to set the boolean value at.
8
+ * @param {number} value - The boolean value to set the `index`.
9
+ * @returns {boolean} The boolean value that was set.
10
+ */
11
+ set(index: number, value: any): boolean;
12
+ /**
13
+ * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
14
+ * @param {number} index - The index to set the boolean value at.
15
+ * @param {number} value - The boolean value to set the `index`.
16
+ * @returns {boolean} The boolean value that was set.
17
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
18
+ */
19
+ setSafe(index: number, value: any): boolean;
20
+ /**
21
+ * Gets a boolean value at the specified index.
22
+ * @param {number} index - The index to get the boolean value of.
23
+ * @returns {boolean} The boolean value that was set.
24
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
25
+ */
26
+ get(index: number): boolean;
27
+ /**
28
+ * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
29
+ * @param {number} index - The index to get the boolean value of.
30
+ * @returns {boolean} The boolean value that was set.
31
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
32
+ */
33
+ getSafe(index: number): boolean;
7
34
  get length(): number;
8
- set length(value: number);
35
+ set length(_value: number);
9
36
  }
10
37
 
11
38
  export { FastBooleanArray as default };
package/dist/index.d.ts CHANGED
@@ -2,10 +2,37 @@ declare class FastBooleanArray {
2
2
  size: number;
3
3
  private buffer;
4
4
  constructor(size: number);
5
- set(input: number, value: boolean): void;
6
- get(input: number): boolean;
5
+ /**
6
+ * Sets a boolean value at the specified index.
7
+ * @param {number} index - The index to set the boolean value at.
8
+ * @param {number} value - The boolean value to set the `index`.
9
+ * @returns {boolean} The boolean value that was set.
10
+ */
11
+ set(index: number, value: any): boolean;
12
+ /**
13
+ * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
14
+ * @param {number} index - The index to set the boolean value at.
15
+ * @param {number} value - The boolean value to set the `index`.
16
+ * @returns {boolean} The boolean value that was set.
17
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
18
+ */
19
+ setSafe(index: number, value: any): boolean;
20
+ /**
21
+ * Gets a boolean value at the specified index.
22
+ * @param {number} index - The index to get the boolean value of.
23
+ * @returns {boolean} The boolean value that was set.
24
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
25
+ */
26
+ get(index: number): boolean;
27
+ /**
28
+ * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
29
+ * @param {number} index - The index to get the boolean value of.
30
+ * @returns {boolean} The boolean value that was set.
31
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
32
+ */
33
+ getSafe(index: number): boolean;
7
34
  get length(): number;
8
- set length(value: number);
35
+ set length(_value: number);
9
36
  }
10
37
 
11
38
  export { FastBooleanArray as default };
package/dist/index.js CHANGED
@@ -6,20 +6,59 @@ var FastBooleanArray = class {
6
6
  this.size = size;
7
7
  this.buffer = new Uint8Array(Math.ceil(size / 8));
8
8
  }
9
- set(input, value) {
9
+ /**
10
+ * Sets a boolean value at the specified index.
11
+ * @param {number} index - The index to set the boolean value at.
12
+ * @param {number} value - The boolean value to set the `index`.
13
+ * @returns {boolean} The boolean value that was set.
14
+ */
15
+ set(index, value) {
10
16
  if (value) {
11
- this.buffer[input >> 3] |= 1 << (input & 7);
17
+ this.buffer[index >> 3] |= 1 << (index & 7);
18
+ return true;
12
19
  } else {
13
- this.buffer[input >> 3] &= ~(1 << (input & 7));
20
+ this.buffer[index >> 3] &= ~(1 << (index & 7));
21
+ return false;
14
22
  }
15
23
  }
16
- get(input) {
17
- return (this.buffer[input >> 3] & 1 << input % 8) !== 0;
24
+ /**
25
+ * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
26
+ * @param {number} index - The index to set the boolean value at.
27
+ * @param {number} value - The boolean value to set the `index`.
28
+ * @returns {boolean} The boolean value that was set.
29
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
30
+ */
31
+ setSafe(index, value) {
32
+ if (index < 0 || index >= this.size) {
33
+ throw new RangeError("Index out of bounds");
34
+ }
35
+ return this.set(index, value);
36
+ }
37
+ /**
38
+ * Gets a boolean value at the specified index.
39
+ * @param {number} index - The index to get the boolean value of.
40
+ * @returns {boolean} The boolean value that was set.
41
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
42
+ */
43
+ get(index) {
44
+ return (this.buffer[index >> 3] & 1 << index % 8) !== 0;
45
+ }
46
+ /**
47
+ * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
48
+ * @param {number} index - The index to get the boolean value of.
49
+ * @returns {boolean} The boolean value that was set.
50
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
51
+ */
52
+ getSafe(index) {
53
+ if (index < 0 || index >= this.size) {
54
+ throw new RangeError("Index out of bounds");
55
+ }
56
+ return this.get(index);
18
57
  }
19
58
  get length() {
20
59
  return this.size;
21
60
  }
22
- set length(value) {
61
+ set length(_value) {
23
62
  throw new Error("Setting the length on BooleanArray's is not supported");
24
63
  }
25
64
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export default class FastBooleanArray {\r\n\tpublic size: number;\r\n\tprivate buffer: Uint8Array;\r\n\r\n\tconstructor(size: number) {\r\n\t\tthis.size = size;\r\n\t\tthis.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n\t}\r\n\r\n\tset(input: number, value: boolean) {\r\n\t\tif (value) {\r\n\t\t\tthis.buffer[input >> 3] |= 1 << (input & 7); // Set bit\r\n\t\t} else {\r\n\t\t\tthis.buffer[input >> 3] &= ~(1 << (input & 7)); // Clear bit\r\n\t\t}\r\n\t}\r\n\r\n\tget(input: number) {\r\n\t\treturn (this.buffer[input >> 3] & (1 << input % 8)) !== 0; // Check bit\r\n\t}\r\n\r\n\tget length() {\r\n\t\treturn this.size;\r\n\t}\r\n\r\n\tset length(value: number) {\r\n\t\tthrow new Error(\"Setting the length on BooleanArray's is not supported\");\r\n\t}\r\n}\r\n"],"mappings":";AAAA,IAAqB,mBAArB,MAAsC;AAAA,EAC9B;AAAA,EACC;AAAA,EAER,YAAY,MAAc;AACzB,SAAK,OAAO;AACZ,SAAK,SAAS,IAAI,WAAW,KAAK,KAAK,OAAO,CAAC,CAAC;AAAA,EACjD;AAAA,EAEA,IAAI,OAAe,OAAgB;AAClC,QAAI,OAAO;AACV,WAAK,OAAO,SAAS,CAAC,KAAK,MAAM,QAAQ;AAAA,IAC1C,OAAO;AACN,WAAK,OAAO,SAAS,CAAC,KAAK,EAAE,MAAM,QAAQ;AAAA,IAC5C;AAAA,EACD;AAAA,EAEA,IAAI,OAAe;AAClB,YAAQ,KAAK,OAAO,SAAS,CAAC,IAAK,KAAK,QAAQ,OAAQ;AAAA,EACzD;AAAA,EAEA,IAAI,SAAS;AACZ,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,IAAI,OAAO,OAAe;AACzB,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACxE;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export default class FastBooleanArray {\r\n\tpublic size: number;\r\n\tprivate buffer: Uint8Array;\r\n\r\n\tconstructor(size: number) {\r\n\t\tthis.size = size;\r\n\t\tthis.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory\r\n\t}\r\n\r\n\t/**\r\n\t * Sets a boolean value at the specified index.\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t */\r\n\tset(index: number, value: any) {\r\n\t\tif (value) {\r\n\t\t\tthis.buffer[index >> 3] |= 1 << (index & 7); // Set bit\r\n\t\t\treturn true;\r\n\t\t} else {\r\n\t\t\tthis.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to set the boolean value at.\r\n\t * @param {number} value - The boolean value to set the `index`.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tsetSafe(index: number, value: any) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.set(index, value);\r\n\t}\r\n\r\n\t/**\r\n\t * Gets a boolean value at the specified index.\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tget(index: number) {\r\n\t\treturn (this.buffer[index >> 3] & (1 << index % 8)) !== 0; // Check bit\r\n\t}\r\n\r\n\t/**\r\n\t * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t * @param {number} index - The index to get the boolean value of.\r\n\t * @returns {boolean} The boolean value that was set.\r\n\t * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).\r\n\t */\r\n\tgetSafe(index: number) {\r\n\t\tif (index < 0 || index >= this.size) {\r\n\t\t\tthrow new RangeError('Index out of bounds');\r\n\t\t}\r\n\t\treturn this.get(index);\r\n\t}\r\n\r\n\tget length() {\r\n\t\treturn this.size;\r\n\t}\r\n\r\n\tset length(_value: number) {\r\n\t\tthrow new Error(\"Setting the length on BooleanArray's is not supported\");\r\n\t}\r\n}\r\n"],"mappings":";AAAA,IAAqB,mBAArB,MAAsC;AAAA,EAC9B;AAAA,EACC;AAAA,EAER,YAAY,MAAc;AACzB,SAAK,OAAO;AACZ,SAAK,SAAS,IAAI,WAAW,KAAK,KAAK,OAAO,CAAC,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,OAAe,OAAY;AAC9B,QAAI,OAAO;AACV,WAAK,OAAO,SAAS,CAAC,KAAK,MAAM,QAAQ;AACzC,aAAO;AAAA,IACR,OAAO;AACN,WAAK,OAAO,SAAS,CAAC,KAAK,EAAE,MAAM,QAAQ;AAC3C,aAAO;AAAA,IACR;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,QAAQ,OAAe,OAAY;AAClC,QAAI,QAAQ,KAAK,SAAS,KAAK,MAAM;AACpC,YAAM,IAAI,WAAW,qBAAqB;AAAA,IAC3C;AACA,WAAO,KAAK,IAAI,OAAO,KAAK;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,OAAe;AAClB,YAAQ,KAAK,OAAO,SAAS,CAAC,IAAK,KAAK,QAAQ,OAAQ;AAAA,EACzD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAQ,OAAe;AACtB,QAAI,QAAQ,KAAK,SAAS,KAAK,MAAM;AACpC,YAAM,IAAI,WAAW,qBAAqB;AAAA,IAC3C;AACA,WAAO,KAAK,IAAI,KAAK;AAAA,EACtB;AAAA,EAEA,IAAI,SAAS;AACZ,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,IAAI,OAAO,QAAgB;AAC1B,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACxE;AACD;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-boolean-array",
3
- "version": "1.1.2",
3
+ "version": "1.3.1",
4
4
  "homepage": "https://github.com/UltraCakeBakery/FastBooleanArray",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.ts CHANGED
@@ -7,23 +7,64 @@ export default class FastBooleanArray {
7
7
  this.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory
8
8
  }
9
9
 
10
- set(input: number, value: boolean) {
10
+ /**
11
+ * Sets a boolean value at the specified index.
12
+ * @param {number} index - The index to set the boolean value at.
13
+ * @param {number} value - The boolean value to set the `index`.
14
+ * @returns {boolean} The boolean value that was set.
15
+ */
16
+ set(index: number, value: any) {
11
17
  if (value) {
12
- this.buffer[input >> 3] |= 1 << (input & 7); // Set bit
18
+ this.buffer[index >> 3] |= 1 << (index & 7); // Set bit
19
+ return true;
13
20
  } else {
14
- this.buffer[input >> 3] &= ~(1 << (input & 7)); // Clear bit
21
+ this.buffer[index >> 3] &= ~(1 << (index & 7)); // Clear bit
22
+ return false;
15
23
  }
16
24
  }
17
25
 
18
- get(input: number) {
19
- return (this.buffer[input >> 3] & (1 << input % 8)) !== 0; // Check bit
26
+ /**
27
+ * like `set` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
28
+ * @param {number} index - The index to set the boolean value at.
29
+ * @param {number} value - The boolean value to set the `index`.
30
+ * @returns {boolean} The boolean value that was set.
31
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
32
+ */
33
+ setSafe(index: number, value: any) {
34
+ if (index < 0 || index >= this.size) {
35
+ throw new RangeError('Index out of bounds');
36
+ }
37
+ return this.set(index, value);
38
+ }
39
+
40
+ /**
41
+ * Gets a boolean value at the specified index.
42
+ * @param {number} index - The index to get the boolean value of.
43
+ * @returns {boolean} The boolean value that was set.
44
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
45
+ */
46
+ get(index: number) {
47
+ return (this.buffer[index >> 3] & (1 << index % 8)) !== 0; // Check bit
48
+ }
49
+
50
+ /**
51
+ * like `get` but throws if the index is out of bounds (less than 0 or greater than or equal to the array size).
52
+ * @param {number} index - The index to get the boolean value of.
53
+ * @returns {boolean} The boolean value that was set.
54
+ * @throws {RangeError} If the index is out of bounds (less than 0 or greater than or equal to the array size).
55
+ */
56
+ getSafe(index: number) {
57
+ if (index < 0 || index >= this.size) {
58
+ throw new RangeError('Index out of bounds');
59
+ }
60
+ return this.get(index);
20
61
  }
21
62
 
22
63
  get length() {
23
64
  return this.size;
24
65
  }
25
66
 
26
- set length(value: number) {
67
+ set length(_value: number) {
27
68
  throw new Error("Setting the length on BooleanArray's is not supported");
28
69
  }
29
70
  }
package/test.js ADDED
@@ -0,0 +1,6 @@
1
+ import BooleanArray from './dist/index.js';
2
+
3
+ const array = new BooleanArray(1);
4
+
5
+ const store = array.set(0, true);
6
+ console.log(array.get(0), store);
package/bench.js DELETED
@@ -1,96 +0,0 @@
1
- import BooleanArray from './dist/index.js';
2
- import { performance } from 'node:perf_hooks';
3
- import { serialize } from 'v8';
4
-
5
- function getMemoryUsageOfReference(ref) {
6
- const serialized = serialize(ref); // Convert the reference to a Buffer
7
- return serialized.length; // The size of the serialized Buffer in bytes
8
- }
9
-
10
- function testVanillaArraySet(amount, analyzeMemory = true) {
11
- const vanillaArray = [];
12
- const startTime = performance.now();
13
- for (let i = 0; i < amount; i++) {
14
- vanillaArray[i] = true;
15
- }
16
- const endTime = performance.now();
17
- const usedMemory = analyzeMemory ? getMemoryUsageOfReference(vanillaArray) : 0;
18
- return { vanillaArray, usedMemory, startTime, endTime };
19
- }
20
- testVanillaArraySet._name = 'Set Vanilla array ';
21
-
22
- function testBooleanArraySet(amount, analyzeMemory = true) {
23
- const theBooleanArray = new BooleanArray(amount);
24
- const startTime = performance.now();
25
- for (let i = 0; i < amount; i++) {
26
- theBooleanArray.set(i, true);
27
- }
28
- const endTime = performance.now();
29
- const usedMemory = analyzeMemory ? getMemoryUsageOfReference(theBooleanArray) : 0;
30
- return { theBooleanArray, usedMemory, startTime, endTime };
31
- }
32
- testBooleanArraySet._name = 'Set Fast Boolean Array';
33
-
34
- function testVanillaArrayGet(amount) {
35
- const { vanillaArray } = testVanillaArraySet(amount, false);
36
- const startTime = performance.now();
37
- for (let i = 0; i < amount; i++) {
38
- vanillaArray[i];
39
- }
40
- const endTime = performance.now();
41
- return {
42
- usedMemory: 0,
43
- startTime,
44
- endTime
45
- };
46
- }
47
- testVanillaArrayGet._name = 'Get Vanilla array ';
48
-
49
- function testBooleanArrayGet(amount) {
50
- const { theBooleanArray } = testBooleanArraySet(amount, false);
51
- const startTime = performance.now();
52
- for (let i = 0; i < amount; i++) {
53
- theBooleanArray.get(i, true);
54
- }
55
- const endTime = performance.now();
56
- return {
57
- usedMemory: 0,
58
- startTime,
59
- endTime
60
- };
61
- }
62
- testBooleanArrayGet._name = 'Get Fast Boolean Array';
63
-
64
- function performanceTest(test, amount) {
65
- const runs = 1000; // Number of times to run the test
66
- let totalTime = 0;
67
- let totalMemory = 0;
68
-
69
- for (let i = 0; i < runs; i++) {
70
- const { startTime, endTime, usedMemory } = test(amount);
71
-
72
- totalTime += endTime - startTime;
73
- totalMemory += usedMemory;
74
- }
75
-
76
- const averageTime = (totalTime / runs).toFixed(8);
77
- const averageMemory = totalMemory / runs;
78
-
79
- console.log(
80
- `${test._name}: ${averageTime} ms | ${test.name.includes('Set') ? averageMemory + ' Bytes' : 'N/A'} | ${amount} indexes`
81
- );
82
- }
83
-
84
- // Run tests
85
- console.clear();
86
- [1, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000].forEach(async (amount) => {
87
- console.log('');
88
- [testVanillaArraySet, testBooleanArraySet, testVanillaArrayGet, testBooleanArrayGet].forEach(
89
- (test) => {
90
- performanceTest(test, amount);
91
- }
92
- );
93
- await new Promise((resolve) => {
94
- setTimeout(resolve, 5_000); // Let garbadge collection do its thing for more accurate memory logging
95
- });
96
- });