Skip to content

Instantly share code, notes, and snippets.

@bartread
Created April 3, 2020 21:04
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/47135e9f5437259d1df60bcb72262024 to your computer and use it in GitHub Desktop.
Save bartread/47135e9f5437259d1df60bcb72262024 to your computer and use it in GitHub Desktop.
'use strict';
(function () {
var RADIANS_IN_A_CIRCLE = 2 * Math.PI;
var ONE_DEGREE = RADIANS_IN_A_CIRCLE / 360;
var lookup = [];
for (var index = 0; index < 360; ++index) {
lookup[index] = Math.tan(index * ONE_DEGREE);
}
doOneMillionLookupsAroundWholeCircle();
doOneMillionLookupsAroundWholeCircle();
doOneMillionCalculationsAroundWholeCircle();
doOneMillionCalculationsAroundWholeCircle();
function doOneMillionLookupsAroundWholeCircle() {
var start = new Date();
var result = 0;
for (
var count = 0;
count < 1000000;
++count
) {
for (
var angleDegrees = 0;
angleDegrees < 360;
++angleDegrees
) {
result = result + lookup[angleDegrees];
}
}
var end = new Date();
var diff = end - start;
console.log(`Tan lookup (ES5) result: ${result}`);
console.log(`Tan lookup (ES5) time: ${diff}`);
}
function doOneMillionCalculationsAroundWholeCircle() {
var start = new Date();
var result = 0;
for (
var count = 0;
count < 1000000;
++count
) {
for (
var angleDegrees = 0;
angleDegrees < 360;
++angleDegrees
) {
result = result + Math.tan(angleDegrees * ONE_DEGREE);
}
}
var end = new Date();
var diff = end - start;
console.log(`Math.tan (ES5) result: ${result}`);
console.log(`Math.tan (ES5) time: ${diff}`);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment