Improve `String.prototype.startsWith` polyfill #554

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
@mathiasbynens
Member

mathiasbynens commented Dec 17, 2013

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

@arv

View changes

src/runtime/runtime.js
+ // 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 (

This comment has been minimized.

@arv

arv Dec 17, 2013

Collaborator

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();
        }
@arv

arv Dec 17, 2013

Collaborator

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();
        }

This comment has been minimized.

@mathiasbynens

mathiasbynens Dec 17, 2013

Member

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.

@mathiasbynens

mathiasbynens Dec 17, 2013

Member

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.

This comment has been minimized.

@arv

arv Dec 17, 2013

Collaborator

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

  1. typeof
  2. Compare the searchString to search
@arv

arv Dec 17, 2013

Collaborator

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

  1. typeof
  2. Compare the searchString to search
@arv

View changes

src/runtime/runtime.js
+ }
+ var start = Math.min(Math.max(pos, 0), stringLength);
+ // Avoid the `indexOf` call if no match is possible
+ if (searchLength + start > stringLength) {

This comment has been minimized.

@arv

arv Dec 17, 2013

Collaborator

This seems like a premature optimization. This seems very rare.

@arv

arv Dec 17, 2013

Collaborator

This seems like a premature optimization. This seems very rare.

This comment has been minimized.

@mathiasbynens

mathiasbynens Dec 17, 2013

Member

If you think so :) I’ll remove it.

@mathiasbynens

mathiasbynens Dec 17, 2013

Member

If you think so :) I’ll remove it.

@arv

This comment has been minimized.

Show comment
Hide comment
@arv

arv Dec 17, 2013

Collaborator

Thanks for taking care of these.

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

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

This comment has been minimized.

Show comment
Hide comment
@mathiasbynens

mathiasbynens Dec 17, 2013

Member

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

Member

mathiasbynens commented Dec 17, 2013

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

@arv

This comment has been minimized.

Show comment
Hide comment
@arv

arv Dec 17, 2013

Collaborator

LGTM

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

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