Add note about function call args limit for using spread syntax to concatenate arrays#43838
Open
Add note about function call args limit for using spread syntax to concatenate arrays#43838
Conversation
…in call arguments limit Array.push mentions using spread syntax to effectively concatenate arrays, but does not note the key limitation that this only works if the array being concatenated has fewer elements than the limit on function call arguments. I've added a note about that to the relevant section.
Update Array.push to note limitation of spread syntax based on functo…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
The Array.push documentation gives an example of using spread syntax to concatenate arrays. However, it doesn't mention the limitation which applies in this case: the array being concatenated needs to be short enough that it doesn't go beyond the limit on the number of arguments to a function call. This adds a note about that to the section with that example, and suggests using Array.concat instead in that case (Array.concat was already linked there as an alternative). It also mentions that Array.concat makes a copy so people who don't follow the link will get a hint about how Array.concat is different from Array.push(...).
Motivation
It's easy in testing to see that Array.push(...x) works if you're testing with a small array x, including in unit tests you might set up. However, in production when x grows larger than the limits on function call parameters (which is pretty easy) this will break. So this is a hidden sharp edge to the suggested concatenation method in the docs. Since Array.concat always makes a copy which is not always desirable due to time/memory overhead, there's an extra incentive to use the push(...) method (and the existing docs don't highlight this distinction on the Array.push side). Adding a note will hopefully help some people avoid a headache.