Skip to content

Instantly share code, notes, and snippets.

@bartread
Last active April 3, 2020 11:18
Show Gist options
  • Select an option

  • Save bartread/a99903bdf486b3a6bf201260faf6b327 to your computer and use it in GitHub Desktop.

Select an option

Save bartread/a99903bdf486b3a6bf201260faf6b327 to your computer and use it in GitHub Desktop.
'use strict';
(function (){
const RADIANS_IN_A_CIRCLE = 2 * Math.PI;
const ONE_DEGREE = RADIANS_IN_A_CIRCLE / 360;
let lookup = [];
for (let index = 0; index < 360; ++index) {
lookup[index] = Math.sin(index * ONE_DEGREE);
}
doOneMillionLookupsAroundWholeCircle();
doOneMillionLookupsAroundWholeCircle();
doOneMillionCalculationsAroundWholeCircle();
doOneMillionCalculationsAroundWholeCircle();
function doOneMillionLookupsAroundWholeCircle() {
let start = new Date();
let result = 0;
for (
let count = 0;
count < 1000000;
++count
) {
for (
let angleDegrees = 0;
angleDegrees < 360;
++angleDegrees
) {
result = result + lookup[angleDegrees];
}
}
let end = new Date();
let diff = end - start;
console.log(`Sin lookup (ES2015+) result: ${result}`);
console.log(`Sin lookup (ES2015+) time: ${diff}`);
}
function doOneMillionCalculationsAroundWholeCircle() {
let start = new Date();
let result = 0;
for (
let count = 0;
count < 1000000;
++count
) {
for (
let angleDegrees = 0;
angleDegrees < 360;
++angleDegrees
) {
result = result + Math.sin(angleDegrees * ONE_DEGREE);
}
}
let end = new Date();
let diff = end - start;
console.log(`Math.sin (ES2015+) result: ${result}`);
console.log(`Math.sin (ES2015+) time: ${diff}`);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment