Performance calculator based on Proxy ES6/ES2015 feature
A Pen by Vlad Bezden on CodePen.
Performance calculator based on Proxy ES6/ES2015 feature
A Pen by Vlad Bezden on CodePen.
'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) |