Resources‎ > ‎Programming‎ > ‎JavaScript‎ > ‎

Performance check

Since JavaScript does not go though the process of compilation, it cannot optimise its execution through a good code optimisation process.

jsPerf provides a playground, where you can test the performance of JavaScript code.  It will gather stats every time someone runs your test code.

For instance, if I provide the following JavaScript and snippets, it automatically generate necessary html file including other required script code to run tests.

<script>
if (typeof DoubleArray != 'undefined') {
glMatrixArrayType = DoubleArray;
} else if (typeof Float64Array != 'undefined') {
glMatrixArrayType = Float64Array;
} else if (typeof FloatArray != 'undefined') {
glMatrixArrayType = FloatArray
} else if (typeof WebGLFloatArray != 'undefined') {
glMatrixArrayType = WebGLFloatArray;
} else {
glMatrixArrayType = Array;
}

vec3 = {};
vec3.create = function(x, y, z) {
var newVec = new glMatrixArrayType(3);
newVec[0] = x;
newVec[1] = y;
newVec[2] = z;
return newVec;
};

vec3.length = function(vec) {
var x = vec[0];
var y = vec[1];
var z = vec[2];
return Math.sqrt(x*x + y*y + z* z);
};

vec3.normalize = function(vec, dest) {
if (!dest) dest = vec;
/*
* why aren't we calling length()?...
* want to avoid overhead...
*/
var x = vec[0], y = vec[1], z = vec[2];
var len = Math.sqrt(x*x + y*y + z* z);

if (!len) {
dest[0] = 0; dest[1] = 0; dest[2] = 0;
} else if (len == 1) {
dest[0] = x; dest[1] = y; dest[2] = z;
} else {
len = 1 / len;
dest[0] = x*len; dest[1] = y*len; dest[2] = z*len;
}

return dest;
};


vec3.normalize2 = function(vec, dest) {
if (!dest) dest = vec;

var len = vec3.length(vec);

if (!len) {
dest[0] = 0; dest[1] = 0; dest[2] = 0;
} else if (len == 1) {
dest[0] = vec[0]; dest[1] = vec[1]; dest[2] = vec[2];
} else {
len = 1 / len;
dest[0] = vec[0]*len; dest[1] = vec[1]*len; dest[2] = vec[2]*len;
}

return dest;
};

</script>

Test 1:
var vec = vec3.create(Math.random(), Math.random(), Math.random());
vec3.normalize(vec);

Test 2:
var vec = vec3.create(Math.random(), Math.random(), Math.random());
vec3.normalize2(vec);

Once, the codes are provided, you can run the test.  It gives the result like:


If you publish your tests, many people could try that and it will gather stats of the test on various platforms.  Then you can see the performance comparison like:



Comments