Original post: https://www.ycmjason.com/blog/2018/04/28.html
this article assumes basic knowledge of RegExp.
Background
I was workin...
For further actions, you may consider blocking this person and/or reporting abuse
Your asynchronous String.prototype.replace function is very nice, but I can see three major ways to massively improve its performance.
It calls await unnecessarily. What it should do is be maybe-asynchronous where it first checks to see if any promises were returned. (See stackoverflow.com/questions/522217...)
The V8 engine has recently upgraded its string concatenation operator, making the need to prepackage strings into lists slower than simple string appendments.
It should use callbacks where possible because as pretty as await/promise/async looks, the W3C really botched their performance. Permanently. Because, await/promise/async are all required to wait until the next tick before executing. What the W3C should have done is add in a
delay
keyword that can be added ontoawait
andasync
fordelay await
anddelay async
, then an extradelay
argument on the Promise and thisdelay
would tell the browser whether or not it really needs to wait until the next tick.Putting the top three together, let us witness my version. Notice how I do not depend on the promise's ability to be deferred to the next tick. This is intentional. If we could all program like this, then the W3C could change its errant standard toward not requiring browsers to delay the process to the next tick.
For more details on why I am making such a huge fuss over having to be delayed to the next tick, please see stackoverflow.com/questions/854777...
Thanks a lot for sharing your inspiring ideas. đģI will keep those in mind in case I encounter any performance issue later. But for now, I would keep my version for readability and elegance. đ
Hi!
Could you give a quick example of the aReplacer function? I can't seem to figure it out
Thanks
It's a function that return a promise of string. It can be any asynchronous operations. What kind of example would you like? I can make something up if you wish.
// put non matching string
substrs.push(str.slice(i, match.index));
what's "i" ??????????
I have edited accordingly. Not sure why this obvious error was never spotted. Thank you!