Skip to content

Improve `String.prototype.startsWith` polyfill #554

Closed
wants to merge 1 commit into from

2 participants

@mathiasbynens

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

@arv arv and 1 other commented on an outdated diff Dec 17, 2013
src/runtime/runtime.js
// Harmony String Extras
// http://wiki.ecmascript.org/doku.php?id=harmony:string_extras
$defineProperties(String.prototype, {
- startsWith: method(function(s) {
- return this.lastIndexOf(s, 0) === 0;
+ // 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 (
@arv
arv added a note Dec 17, 2013

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

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.

@arv
arv added a note Dec 17, 2013

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

  1. typeof
  2. Compare the searchString to search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arv arv and 1 other commented on an outdated diff Dec 17, 2013
src/runtime/runtime.js
+ ) {
+ throw TypeError();
+ }
+ var string = String(this);
+ var stringLength = string.length;
+ var searchString = String(search);
+ var searchLength = searchString.length;
+ var position = arguments[1];
+ // `ToInteger`
+ var pos = position ? Number(position) : 0;
+ if (isNaN(pos)) {
+ pos = 0;
+ }
+ var start = Math.min(Math.max(pos, 0), stringLength);
+ // Avoid the `indexOf` call if no match is possible
+ if (searchLength + start > stringLength) {
@arv
arv added a note Dec 17, 2013

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@arv
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

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

@arv
arv commented Dec 17, 2013

LGTM

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

@arv arv added a commit that referenced this pull request Dec 17, 2013
@mathiasbynens mathiasbynens Improve `String.prototype.startsWith` polyfill
This patch replaces the old `String.prototype.startsWith` polyfill with http://mths.be/startswith, while also adding the tests from that project.

Review URL: #554

Closes #554.
1c6e88d
@arv arv added a commit that closed this pull request Dec 17, 2013
@mathiasbynens mathiasbynens Improve `String.prototype.startsWith` polyfill
This patch replaces the old `String.prototype.startsWith` polyfill with http://mths.be/startswith, while also adding the tests from that project.

Review URL: #554

Closes #554.
1c6e88d
@arv arv closed this in 1c6e88d Dec 17, 2013
@mathiasbynens mathiasbynens added a commit to mathiasbynens/String.prototype.startsWith that referenced this pull request Dec 17, 2013
@mathiasbynens mathiasbynens README: Note that Traceur now uses this polyfill 4d93977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.