Skip to content

Instantly share code, notes, and snippets.

@bartread
Created April 3, 2020 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartread/f72be52be74fafb9feff70560b42b8b5 to your computer and use it in GitHub Desktop.
Save bartread/f72be52be74fafb9feff70560b42b8b5 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.tan(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(`Tan lookup (ES2015+) result: ${result}`);
console.log(`Tan 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.tan(angleDegrees * ONE_DEGREE);
}
}
let end = new Date();
let diff = end - start;
console.log(`Math.tan (ES2015+) result: ${result}`);
console.log(`Math.tan (ES2015+) time: ${diff}`);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment