fast-boolean-array 1.0.0

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.
Files changed (2) hide show
  1. package/index.js +125 -0
  2. package/package.json +18 -0
package/index.js ADDED
@@ -0,0 +1,125 @@
1
+ import { performance } from 'node:perf_hooks';
2
+ import BooleanArrayExisting from 'boolean-array';
3
+ import { serialize } from 'v8';
4
+
5
+ export class BooleanArray {
6
+ constructor(size) {
7
+ this.size = size;
8
+ this.buffer = new Uint8Array(Math.ceil(size / 8)); // Allocate memory
9
+ }
10
+
11
+ // Set a boolean value at a specific index, hex, or RGB
12
+ set(input, value) {
13
+ if (value) {
14
+ this.buffer[input >> 3] |= 1 << (input & 7); // Set bit
15
+ } else {
16
+ this.buffer[input >> 3] &= ~(1 << (input & 7)); // Clear bit
17
+ }
18
+ }
19
+
20
+ // Get a boolean value at a specific index, hex, or RGB
21
+ get(input) {
22
+ return (this.buffer[input >> 3] & (1 << input % 8)) !== 0; // Check bit
23
+ }
24
+
25
+ get length() {
26
+ return this.size;
27
+ }
28
+
29
+ set length(value) {
30
+ throw new Error("Setting the length on BooleanArray's is not supported");
31
+ }
32
+ }
33
+
34
+ function getMemoryUsageOfReference(ref) {
35
+ const serialized = serialize(ref); // Convert the reference to a Buffer
36
+ return serialized.length; // The size of the serialized Buffer in bytes
37
+ }
38
+
39
+ function testVanillaArraySet(amount, analyzeMemory = true) {
40
+ const vanillaArray = [];
41
+ const startTime = performance.now();
42
+ for (let i = 0; i < amount; i++) {
43
+ vanillaArray[i] = true;
44
+ }
45
+ const endTime = performance.now();
46
+ const usedMemory = analyzeMemory ? getMemoryUsageOfReference(vanillaArray) : 0;
47
+ return { vanillaArray, usedMemory, startTime, endTime };
48
+ }
49
+ testVanillaArraySet._name = 'Set Vanilla array ';
50
+
51
+ function testBooleanArraySet(amount, analyzeMemory = true) {
52
+ const theBooleanArray = new BooleanArray(amount);
53
+ const startTime = performance.now();
54
+ for (let i = 0; i < amount; i++) {
55
+ theBooleanArray.set(i, true);
56
+ }
57
+ const endTime = performance.now();
58
+ const usedMemory = analyzeMemory ? getMemoryUsageOfReference(theBooleanArray) : 0;
59
+ return { theBooleanArray, usedMemory, startTime, endTime };
60
+ }
61
+ testBooleanArraySet._name = 'Set Fast Boolean Array';
62
+
63
+ function testVanillaArrayGet(amount) {
64
+ const { vanillaArray } = testVanillaArraySet(amount, false);
65
+ const startTime = performance.now();
66
+ for (let i = 0; i < amount; i++) {
67
+ vanillaArray[i];
68
+ }
69
+ const endTime = performance.now();
70
+ return {
71
+ usedMemory: 0,
72
+ startTime,
73
+ endTime
74
+ };
75
+ }
76
+ testVanillaArrayGet._name = 'Get Vanilla array ';
77
+
78
+ function testBooleanArrayGet(amount) {
79
+ const { theBooleanArray } = testBooleanArraySet(amount, false);
80
+ const startTime = performance.now();
81
+ for (let i = 0; i < amount; i++) {
82
+ theBooleanArray.get(i, true);
83
+ }
84
+ const endTime = performance.now();
85
+ return {
86
+ usedMemory: 0,
87
+ startTime,
88
+ endTime
89
+ };
90
+ }
91
+ testBooleanArrayGet._name = 'Get Fast Boolean Array';
92
+
93
+ function performanceTest(test, amount) {
94
+ const runs = 1000; // Number of times to run the test
95
+ let totalTime = 0;
96
+ let totalMemory = 0;
97
+
98
+ for (let i = 0; i < runs; i++) {
99
+ const { startTime, endTime, usedMemory } = test(amount);
100
+
101
+ totalTime += endTime - startTime;
102
+ totalMemory += usedMemory;
103
+ }
104
+
105
+ const averageTime = (totalTime / runs).toFixed(8);
106
+ const averageMemory = totalMemory / runs;
107
+
108
+ console.log(
109
+ `${test._name}: ${averageTime} ms | ${test.name.includes('Set') ? averageMemory + ' Bytes' : 'N/A'} | ${amount} indexes`
110
+ );
111
+ }
112
+
113
+ // Run tests
114
+ console.clear();
115
+ [1, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000].forEach(async (amount) => {
116
+ console.log('');
117
+ [testVanillaArraySet, testBooleanArraySet, testVanillaArrayGet, testBooleanArrayGet].forEach(
118
+ (test) => {
119
+ performanceTest(test, amount);
120
+ }
121
+ );
122
+ await new Promise((resolve) => {
123
+ setTimeout(resolve, 5_000); // Let garbadge collection do its thing for more accurate memory logging
124
+ });
125
+ });
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "fast-boolean-array",
3
+ "version": "1.0.0",
4
+ "main": "test.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [
9
+ "boolean",
10
+ "array",
11
+ "fast",
12
+ "efficient",
13
+ "map"
14
+ ],
15
+ "author": "Jack van der Bilt",
16
+ "license": "MIT",
17
+ "description": ""
18
+ }