oop vs functional style
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
// array each
Array.prototype.myEachArray = function( fN ){
for( var i = 0, e=this.length; i<e; i++ ){
fN( this[i], i );
}
}
// matrix each
Array.prototype.myEachMatrix = function( fN ){
for( var i = 0, e=this.length; i<e; i++ ){
for( var i2 = 0, e2=this[i].length; i2<e2; i2++ ){
fN( this[i][i2], i, i2 );
}
}
}
// fun style each for array
function myEachArray( a, fN ){
for( var i = 0, e=a.length; i<e; i++ ){
fN( a[i], i );
}
}
// fun style each for matrix
function myEachMatrix( a, fN ){
for( var i = 0, e=a.length; i<e; i++ ){
for( var i2 = 0, e2=a[i].length; i2<e2; i2++ ){
fN( a[i][i2], i, i2 );
}
}
}
// fun style each
function myEach( a, fN ){
if( a.__struct__ === 'matrix' ){
for( var i = 0, e=a.length; i<e; i++ ){
for( var i2 = 0, e2=a[i].length; i2<e2; i2++ ){
fN( a[i][i2], i, i2 );
}
}
}
else if( a.__struct__ === 'array' ){
for( var i = 0, e=a.length; i<e; i++ ){
fN( a[i], i );
}
}
}
array = [1,2,3,4,5,6,7,8,9,10];
sArray = [1,2,3,4,5,6,7,8,9,10];
sArray.__struct__ = 'array';
matrix = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]];
sMatrix = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]];
sMatrix.__struct__ = 'matrix';
sum = 0;
function sumFN( el , i ){ sum += el; }
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
OOP style |
|
pending… |
Function style |
|
pending… |
Neko style |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments