Skip to content

Instantly share code, notes, and snippets.

@brunoais
Created March 2, 2012 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunoais/1957683 to your computer and use it in GitHub Desktop.
Save brunoais/1957683 to your computer and use it in GitHub Desktop.
Testing js performance. Which option is best?
<html>
<head>
<script>
var myfunction = function (){};
(function (w) {
// Define two queues for handlers
w.readyQ = [];
w.bindReadyQ = [];
// Define the fake jQuery function to capture handlers
w.$ = w.jQuery = function (handler) {
// Push a handler into the correct queue
function pushToQ(x, y) {
if (x == "ready") {
w.bindReadyQ.push(y);
} else {
w.readyQ.push(x);
}
}
if (handler === document || handler === undefined) {
// Queue $(document).ready(handler), $().ready(handler)
// and $(document).bind("ready", handler) by returning
// an object with alias methods for pushToQ
return {
ready: pushToQ,
bind: pushToQ
};
} else {
// Queue $(handler)
pushToQ(handler);
}
}
})(window);
</script>
<script>
window.start1 = new Date();
var i = 0;
while(i++ < 50000){
$(document).ready(myfunction);
}
window.end1 = new Date();
</script>
<script>
(function (w) {
// Define two queues for handlers
w.readyQ = [];
w.bindReadyQ = [];
// Push a handler into the correct queue
function pushToQ(x, y) {
if (x == "ready") {
w.bindReadyQ.push(y);
} else {
w.readyQ.push(x);
}
}
var returner = {
ready: pushToQ,
bind: pushToQ
}
// Define the fake jQuery function to capture handlers
w.$ = w.jQuery = function (handler) {
if (handler === document || handler === undefined) {
// Queue $(document).ready(handler), $().ready(handler)
// and $(document).bind("ready", handler) by returning
// an object with alias methods for pushToQ
return returner;
} else {
// Queue $(handler)
pushToQ(handler);
}
}
})(window);
</script>
<script>
window.start2 = new Date();
var i = 0;
while(i++ < 50000){
$(document).ready(myfunction);
}
window.end2 = new Date();
</script>
</head>
<body>
<pre id="result">
Original
Start End Difference
</pre>
<script>
var results = document.getElementById('result').firstChild;
results.data += window.start1.getTime() + " " + window.end1.getTime()+ " " + (window.end1 - window.start1);
results.data += "\r\n Proposed\r\n ";
results.data += window.start2.getTime() + " " + window.end2.getTime()+ " " + (window.end2 - window.start2);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment