Last active
July 26, 2019 21:41
-
-
Save pjones/7fd3db116842327390f9313249554638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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