Skip to content

Instantly share code, notes, and snippets.

@gngenius02
Created November 18, 2019 05: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 gngenius02/3c842e5f46d151f730b012037ecd596c to your computer and use it in GitHub Desktop.
Save gngenius02/3c842e5f46d151f730b012037ecd596c to your computer and use it in GitHub Desktop.
//call the module
const looper = require('./looper')
//create an object to pass to the loop module
const loopOptions = {
fn: () => Math.random(),
max: 1e9
}
looper(loopOptions);
->//{ 'runtime(sec)': 29.4098592 }
module.exports = function(object) {
const { max = 1000000000n, fn } = object;
let counter = 0;
let running = true;
Error.stackTraceLimit = 100;
const A = (fn) => {
fn();
flipper = B;
};
const B = (fn) => {
fn();
flipper = A;
};
let flipper = B;
const then = process.hrtime.bigint();
do {
counter++;
if (counter > max) {
const now = process.hrtime.bigint();
const nanos = now - then;
console.log({ 'runtime(sec)': Number(nanos) / 1000000000.0 });
running = false;
}
flipper(fn);
continue;
} while (running);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment