Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created December 30, 2016 16:50
Show Gist options
  • Save vlad-bezden/e41d105dab05a5dedf61640bdf133f60 to your computer and use it in GitHub Desktop.
Save vlad-bezden/e41d105dab05a5dedf61640bdf133f60 to your computer and use it in GitHub Desktop.
Performance Calculator
'use strict'
console.clear()
function isPrime(number) {
if (number < 2) {
return false
}
const lenth = Math.sqrt(number);
for (let i = 2; i < lenth; i++) {
if (number % i === 0) {
return false
}
}
return true
}
/**
* Calculates execution time of the function
*
* @param {Function} target - function that performance has to be calculated on
* @return - function's (target) return value
*/
function perfCalc(target) {
return new Proxy(target, {
apply: (target, thisArgs, args) => {
const start = performance.now()
const result = target.apply(thisArgs, args)
const end = performance.now()
console.log(`Result: ${result}. Execution Time: ${(end - start).toFixed(4)} ms`)
return result
}
})
}
const timer = perfCalc(isPrime)
timer(1299827)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment