-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
365 lines (335 loc) · 11.4 KB
/
main.js
File metadata and controls
365 lines (335 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const VOWEL_SET = new Set('aeiouAEIOU');
function isVowelBit(c) {
c = c.toLowerCase();
return c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u' ? 1 : 0;
}
const triTable = [false, true, true, true, true, true, true, true];
const experimental = {
tritable: (str) => {
let i = 0;
const len = str.length;
for (; i + 2 < len; i += 3) {
const b1 = isVowelBit(str[i]);
const b2 = isVowelBit(str[i + 1]);
const b3 = isVowelBit(str[i + 2]);
const index = (b1 << 2) | (b2 << 1) | b3;
if (triTable[index]) return true;
}
// leftover 1 or 2 chars:
for (; i < len; i++) {
if (isVowelBit(str[i])) return true;
}
return false;
},
pairTable: (() => {
const T = Array.from({ length: 128 }, () => new Uint8Array(128));
const isV = c => VOWEL_SET.has(c);
for (let i = 0; i < 128; i++) {
for (let j = 0; j < 128; j++) {
T[i][j] = isV(String.fromCharCode(i)) || isV(String.fromCharCode(j)) ? 1 : 0;
}
}
return (str) => {
const len = str.length;
let k = 0;
for (; k + 1 < len; k += 2) {
const a = str.charCodeAt(k);
const b = str.charCodeAt(k + 1);
if (T[a][b]) return true;
}
// Fallback for leftover single char
if (k < len && VOWEL_SET.has(str[k])) return true;
return false;
};
})(),
// Batch-of-4 bitmask processing
batch4Bitmask(str) {
const vowelMask = (1 << 0) | (1 << 4) | (1 << 8) | (1 << 14) | (1 << 20); // a,e,i,o,u bits
const len = str.length;
let i = 0;
while (i < len) {
let chunkMask = 0;
for (let j = 0; j < 4 && i < len; j++, i++) {
const c = str.charCodeAt(i) | 32; // to lowercase
const off = c - 97;
if (off >= 0 && off < 26) {
chunkMask |= (1 << off);
}
}
if (chunkMask & vowelMask) return true;
}
return false;
},
// Bloom-filter style approximate membership + confirm
bloomFilter: (() => {
const M = 64;
const table = new Uint8Array(M);
for (const c of 'aeiouAEIOU') {
table[c.charCodeAt(0) % M] = 1;
}
return (str) => {
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (table[code % M]) {
const lc = String.fromCharCode(code).toLowerCase();
if ('aeiou'.includes(lc)) return true;
}
}
return false;
};
})(),
// Regex in chunks to limit backtracking
regexChunks(str, chunkSize = 32) {
for (let i = 0; i < str.length; i += chunkSize) {
if (/[aeiou]/i.test(str.slice(i, i + chunkSize))) return true;
}
return false;
},
// Buffer scan in Node.js for raw byte check
bufferScan(str) {
const buf = Buffer.from(str, 'utf8');
for (let i = 0; i < buf.length; i++) {
const b = buf[i] | 32;
if (b === 97 || b === 101 || b === 105 || b === 111 || b === 117) return true;
}
return false;
},
}
function generateTestStrings(count, length = 10) {
const vowels = 'aeiou';
const consonants = 'bcdfghjklmnpqrstvwxyz';
const strings = [];
for (let i = 0; i < count; i++) {
let s = '';
const useVowel = Math.random() < 0.5;
for (let j = 0; j < length; j++) {
s += useVowel && j === 0
? vowels[Math.floor(Math.random() * vowels.length)]
: consonants[Math.floor(Math.random() * consonants.length)];
}
strings.push(s);
}
return strings;
}
const methods = {
earlyReturnRegex: (s) => /^[^aeiou]*[aeiou]/i.test(s),
regexMatch: (s) => !!s.match(/[aeiou]/i),
uint8ArraySet: (() => {
const lookup = new Uint8Array(128);
for (const c of 'aeiouAEIOU') lookup[c.charCodeAt(0)] = 1;
return (s) => {
for (let i = 0; i < s.length; i++) {
if (lookup[s.charCodeAt(i)]) return true;
}
return false;
};
})(),
bitmaskArray: (() => {
const map = new Uint8Array(128);
for (const c of 'aeiouAEIOU') map[c.charCodeAt(0)] = 1;
return (s) => {
for (let i = 0; i < s.length; i++) {
if (map[s.charCodeAt(i)]) return true;
}
return false;
};
})(),
bitmaskBits: (str) => {
const bitmask = (1 << 0) | (1 << 4) | (1 << 8) | (1 << 14) | (1 << 20); // aeiou
for (let i = 0; i < str.length; i++) {
const c = str.charCodeAt(i) | 32;
const offset = c - 97;
if (offset >= 0 && offset < 26 && (bitmask & (1 << offset))) return true;
}
return false;
},
charCodeTable: (() => {
const table = new Array(128).fill(false);
for (const c of 'aeiouAEIOU') table[c.charCodeAt(0)] = true;
return (s) => {
for (let i = 0; i < s.length; i++) {
if (table[s.charCodeAt(i)]) return true;
}
return false;
};
})(),
forLoopManual: (s) => {
for (let i = 0; i < s.length; i++) {
const c = s[i].toLowerCase();
if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u') return true;
}
return false;
},
charCodeSwitch: (s) => {
for (let i = 0; i < s.length; i++) {
switch (s.charCodeAt(i)) {
case 65: case 69: case 73: case 79: case 85: // A E I O U
case 97: case 101: case 105: case 111: case 117: // a e i o u
return true;
}
}
return false;
},
regexFirstChar: (s) => /[aeiou]/i.test(s),
regexTest: (s) => /[aeiou]/i.test(s),
indexOf: (s) => 'aeiouAEIOU'.split('').some(v => s.indexOf(v) !== -1),
switchCase: (s) => {
for (let i = 0; i < s.length; i++) {
switch (s[i]) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
return true;
}
}
return false;
},
mapLookup: (() => {
const map = {};
for (const c of 'aeiouAEIOU') map[c] = true;
return (s) => {
for (let i = 0; i < s.length; i++) {
if (map[s[i]]) return true;
}
return false;
};
})(),
bitwiseString: (s) => {
const vowels = 'aeiouAEIOU';
for (let i = 0; i < s.length; i++) {
if (~vowels.indexOf(s[i])) return true;
}
return false;
},
setLookup: (() => {
const set = new Set('aeiouAEIOU');
return (s) => {
for (let i = 0; i < s.length; i++) {
if (set.has(s[i])) return true;
}
return false;
};
})(),
includesLoop: (s) => {
const vowels = 'aeiouAEIOU';
for (let i = 0; i < s.length; i++) {
if (vowels.includes(s[i])) return true;
}
return false;
},
functionalSome: (s) => [...s].some(c => 'aeiouAEIOU'.includes(c)),
// 🔽 NEW METHODS
jumpTable: (() => {
const table = new Array(128).fill(false);
for (const c of 'aeiouAEIOU') table[c.charCodeAt(0)] = true;
return (s) => {
for (let i = 0; i < s.length; i++) {
const code = s.charCodeAt(i);
if (code < 128 && table[code]) return true;
}
return false;
};
})(),
balancedTree: (s) => {
const check = (c) => {
switch (c) {
case 'i': return true;
case 'e': case 'o': return true;
case 'a': case 'u': return true;
default: return false;
}
};
for (let i = 0; i < s.length; i++) {
if (check(s[i].toLowerCase())) return true;
}
return false;
},
lowercaseSetOnce: (s) => {
s = s.toLowerCase();
return s.includes('a') || s.includes('e') || s.includes('i') || s.includes('o') || s.includes('u');
},
...experimental
};
const testCases = [
['hello', true],
['bcdfg', false],
['AEIOU', true],
['XYZ', false],
['', false],
['qwrtyp', false],
['u', true],
['mnopa', true],
['EEE', true],
['bcdefghijklmnopqrstvwxyz', true]
];
console.log('✅ Verifying correctness:');
let allPass = true;
for (const [name, fn] of Object.entries(methods)) {
for (const [input, expected] of testCases) {
const result = fn(input);
if (result !== expected) {
console.error(`❌ ${name} failed for "${input}". Expected ${expected}, got ${result}`);
allPass = false;
break;
}
}
}
if (allPass) console.log('All methods passed the test cases.\n');
console.log('🧪 Benchmarking 10000 random strings (50% with vowels):\n');
const strings = generateTestStrings(1000000);
const timings = [];
for (const [name, fn] of Object.entries(methods)) {
const start = performance.now();
for (const s of strings) fn(s);
const duration = performance.now() - start;
timings.push([name, duration]);
}
timings.sort((a, b) => a[1] - b[1]);
for (const [name, time] of timings) {
console.log(name.padEnd(18), ':', time.toFixed(2), 'ms');
}
function generateCSVPerformanceReport() {
const textLengths = [10, 100, 1000, 10000, 100000];
const testCount = 10000;
const csvRows = [];
// CSV header
csvRows.push(['Method', ...textLengths.map(len => `${len} chars`)].join(','));
// Test each method with different text lengths
for (const [methodName, methodFn] of Object.entries(methods)) {
const rowData = [methodName];
for (const length of textLengths) {
// Generate test strings of this length
const testStrings = generateTestStrings(testCount, length);
// Warm-up run (discard results)
for (const s of testStrings.slice(0, 100)) methodFn(s);
// Actual benchmark
const start = performance.now();
for (const s of testStrings) {
methodFn(s);
}
const duration = performance.now() - start;
// Calculate operations per second
const opsPerSecond = (testCount / (duration / 1000)).toFixed(2);
rowData.push(opsPerSecond);
}
csvRows.push(rowData.join(','));
}
// Write to CSV file
const csvContent = csvRows.join('\n');
fs.writeFileSync('performance_report.csv', csvContent);
console.log('Performance report saved to performance_report.csv');
}
// First verify correctness
console.log('✅ Verifying correctness:');
for (const [name, fn] of Object.entries(methods)) {
for (const [input, expected] of testCases) {
const result = fn(input);
if (result !== expected) {
console.error(`❌ ${name} failed for "${input}". Expected ${expected}, got ${result}`);
allPass = false;
break;
}
}
}
if (allPass) console.log('All methods passed the test cases.\n');
// Generate the performance report
generateCSVPerformanceReport();