What’s the fastest way to JavaScript square a number?
For example, given the number 61, which method performs best? Here are two approaches I have:
function squareIt(number) {
return Math.pow(number, 2);
}
function squareIt(number) {
return number * number;
}
Is there a more efficient method I should consider? I’m looking for the option that the compiler handles best on average, not a shortened version. I also noticed discussions suggesting that squaring a number is faster than multiplying two random numbers, but does this apply to n * n versus math.pow(n, 2)?