Closure vs Global Variable (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<a href="#" id="element1">Click me</a>
<a href="#" id="element2">Click me</a>

Test runner

Ready to run.

Testing in
TestOps/sec
Closure
var obj = {
 init: function() {
  var self = this;
  $('#element1').click(function() {
   self.clickEvent();
  });
 },
 clickEvent: function() {
  this.miscMethod();
 },
 miscMethod: function() {}
};
obj.init();
$('#element1').click();
ready
Global variable
var obj = {
 init: function() {
  $('#element2').click(this.clickEvent);
 },
 clickEvent: function() {
  obj.miscMethod();
 },
 miscMethod: function() {}
};
obj.init();
$('#element2').click();
ready

Revisions

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