Skip to content

Instantly share code, notes, and snippets.

@CezaryDanielNowak
Last active December 19, 2015 09:49
Show Gist options
  • Save CezaryDanielNowak/5935550 to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/5935550 to your computer and use it in GitHub Desktop.
Tool for testing script performance from console. It includes jquery
var testReplies = 10000,
use$ = false,
testCode = function() {
/* code to test */
//when use$ is enabled, $(this) is #actionBox, use it as your container
//#actionBox will be removed after test
},
actionBoxConfigure = function(){
/* put elements into actionBox here */
//var i=100;
//while(i-- > 0)
// $(this).append('<div class="sth">');
};
var testFunction = function() {
var tmpReplies = testReplies,
actionBox = use$ ? jQuery('<div id="actionBox" style="position:absolute;top:-9999px"></div>').appendTo(document.body) : function(){};
actionBoxConfigure.call(actionBox[0]);
var timeStart = new Date().getTime();
if(use$) {
while(tmpReplies-- > 0) {
testCode.call(actionBox[0]);
}
} else {
while(tmpReplies-- > 0) {
testCode()
}
}
var timeEnd = new Date().getTime();
if(use$) {
actionBox.remove()
}
/* OUTPUT: */
var getDate = function() {
var date = new Date();
return date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) +'-'+('0'+date.getDate()).slice(-2)+ ' ' + ('0'+date.getHours()).slice(-2) + ':' + ('0'+date.getMinutes()).slice(-2) + ':' + ('0'+date.getSeconds()).slice(-2);
};
return getDate() + ': '
+ testCode.toSource().length + ' bytes of code, '
+ testReplies + ' replies in '
+ ((timeEnd - timeStart)/1000) + ' seconds';
};
if(!use$ || window.jQuery) {
testFunction();
} else {
var s = document.createElement('script');
s.onerror = function() {
console.log("Can't load jQuery");
};
s.onload = function() {
console.log(testFunction());
};
s.setAttribute('src','http://code.jquery.com/jquery-latest.min.js');
document.getElementsByTagName('head')[0].appendChild(s);
'Loading jQuery...';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment