Last active
November 9, 2024 13:38
-
-
Save nocksock/74f95c2b5c6b6e847854395e36f814ff to your computer and use it in GitHub Desktop.
There's no difference in `a*a` vs `Math.pow(a, 2)` in JS (unlike some other languages)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ deno bench main_bench.ts | |
// benchmark time/iter (avg) iter/s (min … max) p75 p99 p995 | |
// ---------------- ----------------------------- --------------------- -------------------------- | |
// a*a 4.9 ms 204.1 ( 3.8 ms … 9.1 ms) 5.3 ms 8.4 ms 9.1 ms | |
// Math.pow(a, 2) 5.0 ms 200.0 ( 3.8 ms … 11.2 ms) 5.6 ms 8.6 ms 11.2 ms | |
// | |
// a*a 4.8 ms 209.8 ( 3.8 ms … 7.9 ms) 5.2 ms 6.8 ms 7.9 ms | |
// Math.pow(a, 2) 4.8 ms 207.0 ( 3.8 ms … 7.5 ms) 5.2 ms 7.4 ms 7.5 ms | |
// | |
// a*a 4.9 ms 205.9 ( 3.8 ms … 7.8 ms) 5.4 ms 7.7 ms 7.8 ms | |
// Math.pow(a, 2) 4.8 ms 208.3 ( 3.8 ms … 9.7 ms) 5.2 ms 8.8 ms 9.7 ms | |
Deno.bench({ | |
name: 'a*a', | |
fn() { | |
const map = new Map() // storing in a map to prevent code elimination and gc | |
for (let i = 0; i <= 90_000; i++) { | |
map.set(i, i * i) | |
} | |
}, | |
}) | |
Deno.bench({ | |
name: 'Math.pow(a, 2)', | |
fn() { | |
const map = new Map() // storing in a map to prevent code elimination and gc | |
for (let i = 0; i <= 90_000; i++) { | |
map.set(i, Math.pow(i, 2)) | |
} | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment