Created
December 14, 2009 13:35
-
-
Save aarongustafson/256050 to your computer and use it in GitHub Desktop.
Used in combination with the benchmark.js file from JavaScript Performance Rocks
This file contains hidden or 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
<script src="benchmark.js"></script> | |
<h2>classical inheritance vs. closures</h2> | |
<p> | |
<span id="test">test</span> | |
</p> | |
<script type="text/javascript"> | |
// Closure implementation | |
function Pixel(x, y) | |
{ | |
this.x = x; | |
this.y = y; | |
this.getX = function() | |
{ | |
return this.x; | |
} | |
this.getY = function() | |
{ | |
return this.y; | |
} | |
this.setX = function(value) | |
{ | |
this.x = value; | |
} | |
this.setY = function(value) | |
{ | |
this.y = value; | |
} | |
} | |
function testClosure() | |
{ | |
var x = new Pixel( 1, 1 ); | |
} | |
// Prototype implementation | |
function PixelP(x, y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
PixelP.prototype.getX = function() | |
{ | |
return this.x; | |
} | |
PixelP.prototype.getY = function() | |
{ | |
return this.y; | |
} | |
PixelP.prototype.setX = function(value) | |
{ | |
this.x = value; | |
} | |
PixelP.prototype.setY = function(value) | |
{ | |
this.y = value; | |
} | |
function testPrototype() | |
{ | |
var x = new PixelP( 1, 1 ); | |
} | |
</script> | |
<a href="#" onclick="test(10000, testClosure, testPrototype)">run test</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment