Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Maximum call stack size exceeded in encodeImage Function with Large Uint8Array Inputs #89

Open
jzevin opened this issue May 15, 2024 · 0 comments

Comments

@jzevin
Copy link
Contributor

jzevin commented May 15, 2024

The current implementation of the encodeImage function may encounter issues when handling large Uint8Array inputs due to the use of Function.prototype.apply. This can result in a RangeError: Maximum call stack size exceeded or other performance-related problems when the array size exceeds the argument limit of the JavaScript engine.

Steps to Reproduce

  1. Create a large Uint8Array (e.g., 100,000 bytes or more).
  2. Call the encodeImage function with this array.
  3. Observe any errors or performance issues.

Proposed Implementation

async function encodeImage(image: Uint8Array | string): Promise<string> {
  if (typeof image !== 'string') {
    // image is Uint8Array, convert it to base64
    const uint8Array = new Uint8Array(image);
    let binary = '';
    const len = uint8Array.byteLength;
    for (let i = 0; i < len; i++) {
      binary += String.fromCharCode(uint8Array[i]);
    }
    return btoa(binary);
  }
  // the string may be base64 encoded
  return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant