Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Safari call function.apply recursively?

Consider the following:

var foo = []
for (var i=0; i<100000; i++) { foo.push(97); }
var bar = String.fromCharCode.apply(String,foo)

Most browsers run it fine, but Safari throws: RangeError: Maximum call stack size exceeded.

Based on this, it appears that Safari's implementation of Function.prototype.apply is recursive. Is this true?

The MDN page linked above mentions potential issues with the JS engine's argument length limit, but that's clearly not the case here.

EDIT: I still don't think it's an argument length issue. Via this page and my own testing, it looks like Safari can handle up to 524197 arguments, which the above code does not exceed.

Bonus question: We can rewrite the above code to avoid using apply by explicitly calling String.fromCharCode on each element of the array and joining the results together, but I suspect that would be slower (for the browsers that support the large-input apply). What's the best way to assemble a large string from an array of integer character codes?

like image 258
perimosocordiae Avatar asked Mar 14 '26 17:03

perimosocordiae


1 Answers

Apply has limits in some browsers in the length of arguments they accept. Webkit has an observed limit of 2^16, so if you have any need to have more you may want to follow a strategy to break up the arguments. If you read the details of the bug, it's an enforced limitation opposed to it being a problem arising from recursion (the bug in question also threw a similar RangeError).

Anyway, I believe your hunch about string concatenation was correct - join isn't necessarily as good as other methods. Here's a test against string concat where I first break up the arguments (similar to the strategy in the MDN discussion of apply), and it edges out join. Directly adding string together even edged out join, which I'm a little surprised by (in chrome, at least, I'd imagine they must just have some smart gc that can reuse the existing string to great effect, but can say for sure).

Edit - interestingly, it looks like Chrome is the odd one out in terms of how slow join is - for every other browser, it was much closer to concat in terms of performance or even better.

like image 162
Bubbles Avatar answered Mar 16 '26 07:03

Bubbles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!