Return to Snippet

Revision: 43259
at March 20, 2011 08:37 by reelfernandes


Initial Code
var benchmark1;
var benchmark2;
var loop = 1000000
var time 

// test execution time with approach a
time = new Date().getTime()

function ClassA( name )
{
	this.name = name
}

ClassA.prototype.init = function( index )
{
	this.name += index
}

for( var i=0;i<loop;i++ )
{
	var a = new ClassA( 'A' )
		a.init( i )
}

benchmark1 = new Date().getTime() - time


// test execution time with approach b
time = new Date().getTime()

function ClassB( name )
{
	var o = {}
		o.name = name
		o.init = com_classb_init
	return o
}

function com_classb_init( index )
{
	this.name += index
}

for( var i=0;i<loop;i++ )
{
	var b = ClassB( 'B' )
		b.init( i )
}

benchmark2 = new Date().getTime() - time

alert( benchmark1+", "+benchmark2 )

Initial URL
oojs

Initial Description


Initial Title
OOJS - Compare Execution Time of Both Approaches

Initial Tags
javascript, object

Initial Language
JavaScript