Out of curiosity. I check whether some string is longer than a maximum specified length:
var name = "This Is a Name";
if (!name.length >= 10)
{
//valid length
}
else
{
alert("Too long");
}
Is this any better / faster (?):
if (name.length <= 10)
I remember that in some languages it’s better to write the negation first so is it even betterer (yip, I just wrote that) like so (?):
if (10 >= name.length)
I know that 10 is overlapping in the code – don’t mind that. I just want to know if there is any performance / best practice on this.
This is out of your hands, the interpreter will do whatever it deems right with this. Javascript interpreters might be so advanced nowadays they will optimize this, but more likely those interpreters themselves are compiled with a compiler that will have optimized the translation itself so it was even out of the hands of the person that wrote the interpreter.
So really, if you want a language where you want to control this kind of behaviour, you need asm. Not even in (optimized) C do you really control this.
The only thing you can say with certainty is that
n.length > 9could be marginally faster because it contains less characters so it will be parsed quicker. We’re talking nanoseconds, maybe even picoseconds there, though.