Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarongustafson/256050 to your computer and use it in GitHub Desktop.
Save aarongustafson/256050 to your computer and use it in GitHub Desktop.
Used in combination with the benchmark.js file from JavaScript Performance Rocks
<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