Rather than accessing s[front] for each test, especially when the char is not a vowel.
Maybe the js engine already optimizes your code 🤔.
Sure there's an extra affectation which cost time, but s[front] has to be "computed" each time. It's a direct memory access, but first an addition is in order to add front index to the s array adress, right ?
So what is faster:
10 additions + 10 read
or 1 write + 10 read
?
Difference is probably marginal, but wondering how much the assembler code changes. And if the jit optimizes that.
Great post!
For the second problem, i'm wondering if "caching" the char helps:
Rather than accessing
s[front]
for each test, especially when the char is not a vowel.Maybe the js engine already optimizes your code 🤔.
Sure there's an extra affectation which cost time, but
s[front]
has to be "computed" each time. It's a direct memory access, but first an addition is in order to add front index to the s array adress, right ?So what is faster:
Difference is probably marginal, but wondering how much the assembler code changes. And if the jit optimizes that.
Interesting proposal. I ran it ten times on LeetCode and didn't get a faster answer (not that that means anything!).
I ran both functions one million times on a 445-char lorem ipsum text in Chrome latest. It seems that caching the variable is marginally faster 😊