Skip to content

Commit

Permalink
perf: micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jun 24, 2024
1 parent e69dd2b commit a132fdb
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,18 @@ export function countWords(str: string) {

for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
const byteIndex = Math.floor(charCode / BYTE_SIZE);
const byteIndex = (charCode / BYTE_SIZE) | 0;
const bitIndex = charCode % BYTE_SIZE;
const byteAtIndex = BITMAP[byteIndex];
const isMatch = ((byteAtIndex >> bitIndex) & 1) === 1;

// 255 means this is probably a Unicode range match in which case
// we should ignore the value of shouldCount
// @ts-ignore allow JS to naturally coerce boolean into a number
count += isMatch && (shouldCount || byteAtIndex === 255);
if (isMatch && (shouldCount || byteAtIndex === 255)) count++;
shouldCount = !isMatch;
}

// @ts-ignore allow JS to naturally coerce boolean into a number
count += shouldCount;
if (shouldCount) count++;

return count;
}
Expand Down

0 comments on commit a132fdb

Please sign in to comment.