Two years later and your browser most likely supports performance.now()
. To answer the question at hand, how to calculate the average time it takes for a function or code snippet to execute, what I do in the absence of jsPerf is create a test object containing the individual tests I'd like to measure. I then run each test 11 times, recording the last 10 measurements (the first test run acts as a warmup since I notice its time is usually skewed higher). Finally, I divide by 10 to get the average time.
Here's the code:
// Test setup (code shared by all tests)
var date = new Date()
function startTests() {
var sOutput = '';
// Define tests
var oTestSuite = {
'toString': function(bWarmup) {
return doTest(function() {
// Insert code to test below
// ******************************************************************
date.toString().split(' ');
// ******************************************************************
// End code block
});
},
'concatenate': function(bWarmup) {
return doTest(function() {
// Insert code to test below
// ******************************************************************
(''+date).split(' ');
// ******************************************************************
// End code block
});
}
};
for (test in oTestSuite) {
var nTestCount = 10,
sTotalTime = test + 'Total';
sOutput += 'Start timing \'' + test + '\' method...\n';
oTestSuite[sTotalTime] = 0;
oTestSuite[test](); // Warmup
for (var i=0; i<nTestCount; ++i) {
var nTime = oTestSuite[test]();
oTestSuite[sTotalTime] += nTime;
sOutput += 'Took: ' + nTime + ' ms\n';
}
sOutput += test + ' AVERAGE: ' + (oTestSuite[sTotalTime]/nTestCount) +
'\n#################################################################\n';
}
document.getElementById('output').value = sOutput;
}
function doTest(fn) {
var nStart = performance.now();
for (var i=0, imax=100000; i<imax; ++i) {
fn();
}
var nStop = performance.now(),
nTime = nStop-nStart;
return nTime;
}
<textarea id="output" cols="80" rows="26"></textarea><br>
<button id="btn" onclick="startTests()">Start Tests</button>
Here are the results comparing two string conversion techniques in my Chrome v50 (64-bit):
Start timing 'toString' method...
Took: 204.59500000000003 ms
Took: 210.59000000000003 ms
Took: 205.95500000000004 ms
Took: 206.57000000000005 ms
Took: 204.07500000000005 ms
Took: 205.11000000000013 ms
Took: 203.99500000000012 ms
Took: 213.87500000000023 ms
Took: 222.48499999999967 ms
Took: 206.39499999999998 ms
toString AVERAGE: 208.36450000000005
#################################################################
Start timing 'concatenate' method...
Took: 286.18499999999995 ms
Took: 283.6400000000003 ms
Took: 284.8199999999997 ms
Took: 278.5100000000002 ms
Took: 283.0750000000003 ms
Took: 282.5049999999992 ms
Took: 281.3750000000009 ms
Took: 286.59000000000106 ms
Took: 281.3150000000005 ms
Took: 284.09500000000025 ms
concatenate AVERAGE: 283.21100000000024
#################################################################
You can also play around with it in this pen: http://codepen.io/thdoan/pen/aNKJbQ