Skip to content

Commit

Permalink
chore: try out various perf. variants
Browse files Browse the repository at this point in the history
Signed-off-by: Okiki <[email protected]>
  • Loading branch information
okikio committed Mar 3, 2024
1 parent 771420b commit 46c3816
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export async function* asCodePointsIterator<T extends Uint8Array>(
const len = str.length;
while (i < len) {
const codePoint = str.codePointAt(i)!;
if (codePoint === undefined) break; // If codePointAt returns undefined, break the loop.

yield codePoint;
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
}
Expand Down
11 changes: 6 additions & 5 deletions tests/_arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export async function textDecoderArray<T extends Uint8Array>(
const str = utf8Decoder.decode(chunk, { stream: true });

// Extract code points in larger batches
let i = 0;
const size = str.length;
while (i < size) {
const len = str.length;
for (let i = 0; i < len;) {
const codePoint = str.codePointAt(i)!;
arr.push(codePoint);
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
if (codePoint !== undefined) {
arr.push(codePoint);
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/_callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ export async function textDecoderCallback<T extends Uint8Array>(
const str = utf8Decoder.decode(chunk, { stream: true });

// Extract code points in larger batches
let i = 0;
const len = str.length;
while (i < len) {
for (let i = 0; i < len;) {
const codePoint = str.codePointAt(i)!;
cb(codePoint);
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
if (codePoint !== undefined) {
cb(codePoint);
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/_iterators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export async function* textDecoderIterator<T extends Uint8Array>(
const str = utf8Decoder.decode(chunk, { stream: true });

// Extract code points in larger batches
let i = 0;
while (i < str.length) {
const len = str.length;
for (let i = 0; i < len;) {
const codePoint = str.codePointAt(i)!;
yield codePoint;
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
if (codePoint !== undefined) {
yield codePoint;
i += codePoint > 0xFFFF ? 2 : 1; // Adjust index based on code point size
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion tests/mod_bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { asCodePointsIterator, asCodePointsCallback } from "../mod.ts";
import { asCodePointsArray, asCodePointsIterator, asCodePointsCallback } from "../mod.ts";

Deno.bench("asCodePoints Array - benchmark", async () => {
const iterable = (async function* () {
yield new Uint8Array([0x61, 0xC3, 0xA9, 0xE0, 0xA4, 0xB9, 0xF0, 0x9F, 0x98, 0x82]);
})();
const result: number[] = await asCodePointsArray(iterable);
});

Deno.bench("asCodePoints Iterator - benchmark", async () => {
const iterable = (async function* () {
Expand Down

0 comments on commit 46c3816

Please sign in to comment.