Square Perf

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
ChatGpt Square Algorithm
const square = (number) => {
    let result = 1;
    let base = number;
    let exponent = 2; // Squaring, so the exponent is 2

    while (exponent > 0) {
        if (exponent & 1) {
            result *= base;
        }
        base *= base;
        exponent >>= 1; // Right shift the exponent to process the next bit
    }

    return result;
};

console.log(square(999_999_999)); // Output: 25 (5 squared)
ready
JS Math.pow()
console.log(Math.pow(999_999_999, 2));
ready
JS **
console.log(999_999_999**2);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.