-
Notifications
You must be signed in to change notification settings - Fork 247
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
String.prototype.repeat - resolves #78 #79
base: dev
Are you sure you want to change the base?
Conversation
Can you link where you pulled the code from? I can't seem to find it with a quick Google search |
This is new code based on the ECMA 6 draft spec: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.repeat I obtained the meat of the string-concatenating code from this jsperf test: http://jsperf.com/repeat-string-n-times |
Does this work back to IE8? Have a way to test this out please? |
You can simply include the raw file on a page and load it in IE8. The script running will be the updated version, but it will load the normal Spritz CSS, etc. |
I tested it in Chrome by modifying the bookmarklet. |
Fast, but let's go faster ^ |
The checking against NaN, Infinity, <0, and Math.floor/Math.abs are necessary for the ECMA 6 compliance. That'll still be faster than the current Spritz method, though. Also, following the spec means we can expect the same behavior (but faster) when a browser implements the method natively (at least with a mature implementation). |
Cool, so why strict ES6 compliance really needed? Are the results
|
You may not have received my previous edit in your email. I think this explains it quite well: "Also, following the spec means we can expect the same behavior (but faster) when a browser implements the method natively (at least with a mature implementation)." |
I would hope so; there should be alot of redundant But there still ES5 native functions that are faster if you re-write in ES3, like If those specific checks are really needed, sure let them be used. But otherwise, why make slow down peoples' browsers & run down their battery more? Also, it is unlikely majority of mobile browsers will have ES6, so I'd prefer my version as a shim. |
It's a tradeoff between performance and human efficiency. People who use the function without seeing the source may assume it works like the ECMA 6 spec. It may befit this project to use a different function altogether and avoid changing the native String object. |
I would advise against overriding functions that have a spec without also following the spec, lest you reduce interoperability with other JavaScript libraries. |
Totally agree; I forgot to consider how that function was called. Thanks for pointing that out! BTW, I think your routine is faster than the current es6-shim. I wish I had time to to a PR for you (funeral & looking for employment). But if you could please make a PR & jsPerf test to prove that your 100% spec version is faster, I'm sure many would benefit from your wisdom. |
It looks like the es6-shim is actually faster, so perhaps we should consider switching to that algorithm regardless of whether we actually use String.prototype.repeat. |
I concur with the es6-shim strategy, especially the "repeat" sub function. However I also agree with [Barbarrosa] that we should avoid overriding functions why not just call this "repeat" sub function as a standalone function? |
Do you have a perf for that please? I could not find a perf to prove that; I looked for one & don't have the time to make it. |
No description provided.