Javascript performance summary

I have an idea that Javascript could allow us to improve perfomance of our web applications. Of cause JS is very slow but unlike server side code we can run it on side of our clients. Something like client side cloud-computing.
It is jut an idea… for now.
Thing i can do right now is to improve performance of my scripts as much as possible. A lot can be gained just by following some coding style rules.

Javascript performance. Array vs Object

Reading and writing perfomance is not differs too much for this two. So you can easily use Object for mapping purposes and Array for unordered stuff.

details:

I have coded a simple profiler to test some siple pieces of code. Here it is:

function Profile(){

}

Profile.prototype.status = 0;//0 free, 1 busy
Profile.prototype.tests = [];

Profile.prototype.testIt = function(){
    if (!this.status && this.tests.length > 0) {
        var atest = this.tests.shift();
        var that = this;
        var l = setTimeout(function(){
            that.process(atest[0], atest[1], atest[2]);
            that.testIt();
        }, 500);
        this.status = 1;
    }
}

Profile.prototype.profile = function(func, name, before){
    Profile.prototype.tests.push([func, name, before]);
    this.testIt();
}
Profile.prototype.process = function(func, name, before){
    var AVGtime = 0;
    for (var i = 0; i < 20; i++) {
        var scope = {};
        before.call(scope);

        var dbefore = new Date();
        func.call(scope);
        var dafter = new Date();

        AVGtime += dafter.getTime() - dbefore.getTime();
    }
    this.report(func, AVGtime / 20, name);
    this.status = 0;
}
Profile.prototype.report = function(func, avg, name){
    var funcDiv = document.createElement("div");
    var report = "<div class='function' style='font-size: small; border: 1px solid #999; margin: 10px 0 3px 10px; padding: 5px;'>" + func.toString() + "</div";
    report += "<div class='avgTime' style='font-weight: bold;margin: 0 0 0 10px'>" + name + ": " + avg + "ms</div>";
    funcDiv.innerHTML = report;
    document.body.appendChild(funcDiv);
}

Here are the rules and results of all perfomance tests i run.

Javascript performance. Array vs Object

This entry was posted in post and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>