Skip to content

Instantly share code, notes, and snippets.

@pjones
Last active July 26, 2019 21:41
Show Gist options
  • Save pjones/7fd3db116842327390f9313249554638 to your computer and use it in GitHub Desktop.
Save pjones/7fd3db116842327390f9313249554638 to your computer and use it in GitHub Desktop.
// The following function is a combinator. It takes a function as an
// argument, and returns a function.
function timed(subject) {
return function(...args) {
const start = Date.now();
const result = subject.apply(null, args);
const end = Date.now();
console.log("timed function took: " + (end - start) + "ms");
return result;
};
}
function sillyFunction() {
for (let i=0; i<10000000; ++i) {
// Taking the CPU for a spin.
}
}
const timedSillyFunction = timed(sillyFunction);
timedSillyFunction();
const timedAdder = timed(n => n + 1);
const r1 = timedAdder(1); // 2
const r2 = timedAdder(19); // 20
console.assert(r1 === 2);
console.assert(r2 === 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment