Skip to content

Instantly share code, notes, and snippets.

@pjones
Last active Jul 26, 2019
Embed
What would you like to do?
// 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