Last active
September 13, 2022 23:16
-
-
Save chenglou/ff603736ae48c09c9293651dc94a6adf to your computer and use it in GitHub Desktop.
JSBench
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
function pointsOnEllipse1(cx, cy, rx, ry, n) { | |
const points = Array(n); | |
const step = Math.PI / (n / 2); | |
let sin = 0; | |
let cos = 1; | |
const a = Math.cos(step); | |
const b = Math.sin(step); | |
for (let i = 0; i < n; i++) { | |
points[i] = { x: cx + rx * cos, y: cy + ry * sin }; | |
const ts = b * cos + a * sin; | |
const tc = a * cos - b * sin; | |
sin = ts; | |
cos = tc; | |
} | |
return points; | |
} | |
function pointsOnEllipse2(cx, cy, rx, ry, n) { | |
const xs = Array(n); | |
const ys = Array(n); | |
const step = Math.PI / (n / 2); | |
let sin = 0; | |
let cos = 1; | |
const a = Math.cos(step); | |
const b = Math.sin(step); | |
for (let i = 0; i < n; i++) { | |
xs[i] = cx + rx * cos | |
ys[i] = cy + ry * sin | |
const ts = b * cos + a * sin; | |
const tc = a * cos - b * sin; | |
sin = ts; | |
cos = tc; | |
} | |
return {xs, ys}; | |
} |
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
let results1 = []; | |
for (let i = 0; i < 999; i++) { | |
results1.push( | |
pointsOnEllipse1(Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), 200) | |
) | |
} | |
let result = 0 | |
for (let i = 0; i < results1.length; i++) { | |
let r = results1[i] | |
for (let j = 0; j < r.length; j++) { | |
result += r[j].x + r[j].y | |
} | |
} | |
console.log(result) |
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
let results2 = []; | |
for (let i = 0; i < 999; i++) { | |
results2.push( | |
pointsOnEllipse2(Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), 200) | |
) | |
} | |
let result = 0 | |
for (let i = 0; i < results2.length; i++) { | |
let r = results2[i] | |
for (let j = 0; j < r.xs.length; j++) { | |
result += r.xs[j] + r.ys[j] | |
} | |
} | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment