Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #247
This PR was written by the language model gods. Here's what they have to say about the reasoning behind the fix:
The error "RangeError: Maximum call stack size exceeded" in the original function arises due to the use of
String.fromCharCode.apply(null, new Uint8Array(bytes))
. This method attempts to convert aUint8Array
into a string by applyingString.fromCharCode
to each byte in the array. However, when theUint8Array
is large, this approach leads to a very large number of arguments being passed toString.fromCharCode
via theapply
method, which exceeds the JavaScript engine's maximum call stack size.JavaScript has a limit on the number of arguments that can be passed to a function, and exceeding this limit results in a stack overflow error, as seen here. This is particularly problematic with large binary data like images, where the byte array can be very large.
The refactored bytesToBase64 function avoids this issue by iteratively building a string from the Uint8Array using a loop. In each iteration, it converts a single byte to a character using String.fromCharCode and appends this character to a string. This method does not involve passing a large number of arguments to a function at once, thus it stays within the call stack limit and avoids the error.
This approach is more efficient in terms of memory usage and avoids the call stack limit issue, making it suitable for handling large data arrays.