Created
April 20, 2011 01:25
-
-
Save unicornrainbow/930146 to your computer and use it in GitHub Desktop.
Profile $(this) vs $this
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<title>jQuery $this Test</title> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(function(){ | |
$("#test1").click(function(){ | |
var t0 = new Date | |
var somthing; | |
for(var i=1; i < 10000000; i++){ | |
somthing = $(this); | |
} | |
alert(new Date - t0); // => ~2000ms in my test | |
}); | |
$("#test2").click(function(){ | |
var t0 = new Date | |
var $this = $(this); | |
var somthing; | |
for(var i=1; i < 10000000; i++){ | |
somthing = $this; | |
} | |
alert(new Date - t0); // => ~37ms in my test | |
}); | |
$("#test3").click(function(){ | |
var t0 = new Date | |
var $this = $(this); | |
var somthing; | |
for(var i=1; i < 100; i++){ | |
somthing = $this; | |
} | |
alert(new Date - t0); | |
}); | |
$("#test4").click(function(){ | |
var t0 = new Date | |
var $this = $(this); | |
var somthing; | |
for(var i=1; i < 100; i++){ | |
somthing = $this; | |
} | |
alert(new Date - t0); | |
}); | |
}) | |
</script> | |
</head> | |
<body> | |
<input id="test1" type="button" value="Create $(this) 10,000,000 times"/> | |
<input id="test2" type="button" value="Create $(this) once and access 10,000,000 times"/> | |
<input id="test3" type="button" value="Create $(this) 100 times"/> | |
<input id="test4" type="button" value="Create $(this) once and access 100 times"/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment