Variable outside faster (v10)

Revision 10 of this benchmark created on


Description

Check whether declaring a variable outside or inside a loop is faster.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class=copy></div><div class=copy></div><div class=copy></div>
<div class=copy></div><div class=copy></div><div class=copy></div>
<div class=copy></div><div class=copy></div><div class=copy></div>
<div class=copy></div><div class=copy></div><div class=copy></div>

Test runner

Ready to run.

Testing in
TestOps/sec
inside
$('.copy').each(function() {
  var i = $(this).clone().removeClass('copy');
  $('body').append(i);
});
ready
outside
var i = null;
$('.copy').each(function() {
  i = $(this).clone().removeClass('copy');
  $('body').append(i);
});
ready
Optimized
$('.copy').each(function() {
  $('body').append($(this).clone().removeClass('copy'));
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.