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

Improve String.prototype.startsWith polyfill #554

Closed
wants to merge 1 commit into from
Closed

Improve String.prototype.startsWith polyfill #554

wants to merge 1 commit into from

Conversation

mathiasbynens
Copy link
Member

This patch replaces the old String.prototype.startsWith polyfill with http://mths.be/startswith, while also adding the tests from that project.

// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.startswith
startsWith: method(function(search) {
/*! http://mths.be/startswith v0.1.0 by @mathias */
if (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange indentation on these lines.

Also, is the check for search really needed? The common case is that search is provided and if it is falsey it will fail on the toString check anyway.

        if (this == null || $toString.call(search) == '[object RegExp]') {
          throw TypeError();
        }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid the toString.call whenever possible for performance reasons, but yeah, in the common case it won’t make a difference. I’ll make the change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably skip it in the common case by doing one of.

  1. typeof
  2. Compare the searchString to search

@arv
Copy link
Collaborator

arv commented Dec 17, 2013

Thanks for taking care of these.

I was secretly hoping you would take care of these (and StringIterator, hint hint)

@mathiasbynens
Copy link
Member Author

You’re welcome! Amended the commit as per your remarks.

@arv
Copy link
Collaborator

arv commented Dec 17, 2013

LGTM

At this point I don't think we need to get rid of the toString call.

@arv arv closed this in 1c6e88d Dec 17, 2013
mathiasbynens added a commit to mathiasbynens/String.prototype.startsWith that referenced this pull request Dec 17, 2013
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

Successfully merging this pull request may close these issues.

2 participants