Skip to content

Instantly share code, notes, and snippets.

@jtara1
Created February 6, 2019 18:50
Show Gist options
  • Save jtara1/ffa3f503f672c5506fcaaf164b54b837 to your computer and use it in GitHub Desktop.
Save jtara1/ffa3f503f672c5506fcaaf164b54b837 to your computer and use it in GitHub Desktop.
basic decorator examples
function timeIt(func) {
return async (...args) => {
const start = new Date();
const output = await func(...args);
console.log(`${func.name} took ${(new Date() - start) / 1000} s`);
return output;
}
}
const sleep = async (seconds) => new Promise(resolve => setTimeout(seconds * 1000, resolve));
async function myFunc() {
await sleep(2);
return true;
}
myFunc = timeIt(myFunc);
// run it
(async () => {
await myFunc();
})();
from time import sleep, time
def time_it(func):
def run(*args, **kwargs):
start = time()
output = func(*args, **kwargs)
print(f'{func.__name__} took {time() - start} s')
return output
return run
@time_it
def my_func():
sleep(2)
return True
my_func() # run it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment