fast-boolean-array 1.1.0 → 1.1.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jack van der Bilt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # Fast Boolean Array
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.
4
+
5
+ View detailed benchmark results below.
6
+
7
+ ## Features
8
+
9
+ - **Memory Efficiency**: Stores booleans in bits rather than bytes, drastically reducing memory usage.
10
+ - **Simple API**: Intuitive methods for setting and getting values.
11
+ - **Performance**: Optimized for use in scenarios where memory constraints are critical.
12
+
13
+ ---
14
+
15
+ ## Installation
16
+
17
+ Install the package via npm:
18
+ ```bash
19
+ npm install fast-boolean-array
20
+ ```
21
+
22
+ ---
23
+
24
+ ## Usage
25
+
26
+ Here's how to use the Fast Boolean Array:
27
+
28
+ ```javascript
29
+ const BooleanArray = require('fast-boolean-array');
30
+
31
+ // Create a new BooleanArray with the desired size
32
+ const booleans = new BooleanArray(10);
33
+
34
+ // Set a value at a specific index
35
+ 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
+ booleans.set(1, false);
42
+
43
+ // Retrieve it
44
+ console.log(booleans.get(1)); // Output: false
45
+ ```
46
+
47
+ ---
48
+
49
+ ## API
50
+
51
+ ### `new BooleanArray(size)`
52
+ Creates a new boolean array of the given size. All values are initialized to `false`.
53
+
54
+ - **Parameters:**
55
+ - `size` (number): The number of booleans the array should hold.
56
+
57
+ ### `set(index, value)`
58
+ Sets the boolean value at the specified index.
59
+
60
+ - **Parameters:**
61
+ - `index` (number): The position in the array to set the value.
62
+ - `value` (boolean): The value to set (`true` or `false`).
63
+
64
+ ### `get(index)`
65
+ Gets the boolean value at the specified index.
66
+
67
+ - **Parameters:**
68
+ - `index` (number): The position in the array to retrieve the value.
69
+
70
+ - **Returns:**
71
+ - `boolean`: The value at the given index.
72
+
73
+ ---
74
+
75
+ ## Why Use Fast Boolean Array?
76
+
77
+ - **Efficient Memory Usage**: Ideal for applications handling large boolean arrays, such as data compression or state tracking.
78
+ - **Easy to Use**: Familiar `Map` and `Set` like API for minimal learning curve.
79
+ - **Scalable**: Suitable for high-performance, memory-sensitive projects.
80
+
81
+ ---
82
+
83
+ # Fast Boolean Array vs Vanilla JavaScript Array Performance
84
+
85
+ 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.
86
+
87
+ ## Performance Breakdown
88
+
89
+ ### 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 |
96
+
97
+ **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
+
99
+ ### 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 |
106
+
107
+ **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
+
109
+ ### 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 |
116
+
117
+ **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
+
119
+ ### 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 |
126
+
127
+ **Observation:** For 10,000 booleans, Fast Boolean Array is **3x** faster in set operations and uses **8x** less memory as expected.
128
+
129
+ ### 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 |
136
+
137
+ **Observation:** At 100,000 booleans, Fast Boolean Array is approximately **6.5x** faster in set operations and uses **8x** less memory as expected.
138
+
139
+ ### 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 |
146
+
147
+ **Observation:** For 1,000,000 booleans, Fast Boolean Array is nearly **10x** faster in set operations and uses **8x** less memory as expected.
148
+
149
+ ### 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 |
156
+
157
+ **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
+
159
+ ## Summary
160
+ 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
+
162
+ ---
163
+
164
+ ## Contributing
165
+
166
+ Contributions are welcome! If you'd like to report a bug, suggest a feature, or submit a pull request, please visit the [GitHub repository](https://github.com/yourusername/fast-boolean-array).
167
+
168
+ ---
169
+
170
+ ## License
171
+
172
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
173
+
174
+ ---
175
+
176
+ ## Acknowledgements
177
+
178
+ Thank you to the open-source community for inspiration and support!
179
+
package/bench.js CHANGED
@@ -1,4 +1,4 @@
1
- import BooleanArray from 'boolean-array';
1
+ import BooleanArray from './dist/index.js';
2
2
  import { performance } from 'node:perf_hooks';
3
3
  import { serialize } from 'v8';
4
4
 
package/dist/index.cjs CHANGED
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- FastBooleanArray: () => FastBooleanArray
23
+ default: () => FastBooleanArray
24
24
  });
25
25
  module.exports = __toCommonJS(src_exports);
26
26
  var FastBooleanArray = class {
@@ -47,8 +47,4 @@ var FastBooleanArray = class {
47
47
  throw new Error("Setting the length on BooleanArray's is not supported");
48
48
  }
49
49
  };
50
- // Annotate the CommonJS export names for ESM import in node:
51
- 0 && (module.exports = {
52
- FastBooleanArray
53
- });
54
50
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export 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;AAAO,IAAM,mBAAN,MAAuB;AAAA,EACtB;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\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":[]}
package/dist/index.d.cts CHANGED
@@ -8,4 +8,4 @@ declare class FastBooleanArray {
8
8
  set length(value: number);
9
9
  }
10
10
 
11
- export { FastBooleanArray };
11
+ export { FastBooleanArray as default };
package/dist/index.d.ts CHANGED
@@ -8,4 +8,4 @@ declare class FastBooleanArray {
8
8
  set length(value: number);
9
9
  }
10
10
 
11
- export { FastBooleanArray };
11
+ export { FastBooleanArray as default };
package/dist/index.js CHANGED
@@ -24,6 +24,6 @@ var FastBooleanArray = class {
24
24
  }
25
25
  };
26
26
  export {
27
- FastBooleanArray
27
+ FastBooleanArray as default
28
28
  };
29
29
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export 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":";AAAO,IAAM,mBAAN,MAAuB;AAAA,EACtB;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\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":[]}
package/package.json CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "fast-boolean-array",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
+ "homepage": "https://github.com/UltraCakeBakery/FastBooleanArray",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/UltraCakeBakery/FastBooleanArray.git"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/UltraCakeBakery/FastBooleanArray/issues",
11
+ "email": "fast-boolean-array@managing.software"
12
+ },
4
13
  "type": "module",
5
14
  "main": "./dist/index.cjs",
6
15
  "module": "./dist/index.js",
@@ -16,6 +25,8 @@
16
25
  }
17
26
  },
18
27
  "scripts": {
28
+ "check": "npm run check:types",
29
+ "check:types": "npx --yes @arethetypeswrong/cli --pack .",
19
30
  "build": "tsup src/index.ts --dts --format cjs,esm --clean --sourcemap"
20
31
  },
21
32
  "keywords": [
@@ -25,10 +36,13 @@
25
36
  "efficient",
26
37
  "map"
27
38
  ],
28
- "author": "Jack van der Bilt",
39
+ "author": "Jack van der Bil <jack@managing.software> (https://jackvanderbilt.nl)",
29
40
  "license": "MIT",
30
41
  "description": "",
31
- "dependencies": {
42
+ "engines": {
43
+ "node": ">=0.10.3"
44
+ },
45
+ "devDependencies": {
32
46
  "tsup": "^8.3.5"
33
47
  }
34
48
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export class FastBooleanArray {
1
+ export default class FastBooleanArray {
2
2
  public size: number;
3
3
  private buffer: Uint8Array;
4
4