Skip to content

Instantly share code, notes, and snippets.

@ajklein
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajklein/335e0f948c500a0c25dc to your computer and use it in GitHub Desktop.
Save ajklein/335e0f948c500a0c25dc to your computer and use it in GitHub Desktop.
String.prototype.split and its `limit` argument

String.prototype.split ( separator, limit )

Splits the receiver at separator, returns an Array of at most limit segments.

ES5 (15.5.4.14.5): If limit is undefined, let lim = 232-1; else let lim = ToUint32(limit).

ES6 (21.1.3.17.8): If limit is undefined, let lim = 253-1; else let lim = ToLength(limit).

Two problems

  • Return value is an Array, so a limit greater than 232-1 would result in a "malformed" array (one with elements past the end of the array). Iteration over the return value will skip all such elements.
  • Behavior changes for negative limit: ToUint32 transforms -1 to 232-1; ToLength instead transforms -1 to 0.

Proposal

Revert this spec change. Existing implementations still match ES5, and the old behavior still makes sense (even with ES6's longer String length limit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment