Skip to content

Instantly share code, notes, and snippets.

@bartread
Created April 3, 2020 20:35
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/f1160621a59fc8e2fb97be72a04094a0 to your computer and use it in GitHub Desktop.
Save bartread/f1160621a59fc8e2fb97be72a04094a0 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.cos(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(`Cos lookup (ES2015+) result: ${result}`);
console.log(`Cos 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.cos(angleDegrees * ONE_DEGREE);
}
}
let end = new Date();
let diff = end - start;
console.log(`Math.cos (ES2015+) result: ${result}`);
console.log(`Math.cos (ES2015+) time: ${diff}`);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment