Replace startsWith in the toVdom function#99
Replace startsWith in the toVdom function#99cbravobernal merged 3 commits intomain-wp-directives-pluginfrom
startsWith in the toVdom function#99Conversation
|
Hmm, I'm not sure what happened here - I can't reproduce this bug anymore and I also notice that the regex should never even fail because it's inside of I think the only case it would fail is if there were an attribute like I ll have to investigate tomorrow because it's quite late now in my timezone 😅. |
|
|
|
To make this as close as possible to what we will ship in the future, I'd use Webpack to do the build and I'd start using esmodules in |
|
The
What I meant here is that this regex would fail if we had an an attribute like const name = 'wp-'
/wp-([^:]+):?(.*)$/.exec(name); # returns null
👍 Agreed on this one! |
|
Maybe we can replace this line: if (name.startsWith('wp-')) {with if (name.startsWith('wp-') && name[3]) {It should still be pretty fast. |
|
FYI: It seems the site 24life.ro is facing this issue. It seems to be solved with the current status of this PR. |
|
I've tested these options: // 1
name.startsWith('wp-') && name[3]
// 2
name[0] === "w" && name[1] === "p" && name[2] === "-" && name[3]
// 3
const rg = /wp-\w/
rg.test(name);Benchmark here: https://jsbench.me/9ilb2g41nb/1 3 is the slowest in every browser. 1 and 2 are equivalent in Chrome and Firefox, but 2 is twice as fast in Safari. Actually, looking at the results, it seems like Unless someone else has another idea, let's use 2 for now. |
It seems like they are replacing |
!== null in toVdom() startsWith in the toVdom function
…fix/regex-destructuring
|
I've made the change. Ready for review 🙂 |
|
I'm fine adding these changes as it seems to be more performant. However, are we supposed to give this kind of support? If a site modifies |
|
I can't approve it because it's my own PR. I agree that we shouldn't have to worry about cases when native methods are modified but I approve anyway because it is faster in the end (thanks for pushing this forward @luisherranz! 🙂) |
SantosGuillamot
left a comment
There was a problem hiding this comment.
I've tested it, and it looks to be working fine. Thanks for the fix! 🙂
I agree with those statements, but I'd like to be extra careful with our |
The result of
RegExp.prototype.exec()can be null so we have to handle it.PS. This was causing an error while running some of our stress-tests which is how I found it 🙂