Forked from AllThingsSmitty/function-performance-test.js
Created
November 2, 2018 11:33
-
-
Save DerZyklop/1a0a68d2a67f033f8d13020ac13b0134 to your computer and use it in GitHub Desktop.
A quick JavaScript function performance test on the browser console
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
var i = performance.now(); | |
yourFunction(); | |
performance.now() - i; | |
//Or make a helper function, like this: | |
function performanceTest(testFunction, iterations) { | |
'use strict'; | |
var sum = 0; | |
var start = performance.now(); | |
for (var i = 0; i < iterations; i++) { | |
testFunction(); | |
} | |
var time=performance.now() - start; | |
return time; | |
} | |
//And use it like this: | |
performanceTest(function(){ | |
Math.random()*Math.random(); | |
}, 1000); | |
//In NodeJS you would need to use process.hrtime() instead of performance.now() and it behaves a little differently. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment