📜 ⬆️ ⬇️

Getting rid of dead code in Javascript in IE9

[From the translator: this translation is part of this official post from the blog of the IE team and is intended to clarify a recent misunderstanding: IE9 - Cheating on SunSpider JS? ]

One of the changes in our new JavaScript engine, code-named Chakra, is the destruction of dead code, in order to improve the performance of real sites. Yesterday afternoon, someone posted a question with us on the connection - "Sampling" . Since many people are interested in this question, this blog post is designed to answer it.


In short, the JavaScript IE9 engine has undergone a huge variety of changes in order to improve the performance of real web applications. You can try some sample applications on our website ietestdrive.com , and then compare the results in different browsers. The behavior of our JS engine is not a “specially tweaked” optimization and this is not a bug.
')
In Chakra, we applied some optimizations that are well known in the compiler world, in particular the elimination of dead code . This type of optimization scans the program code in search of code that does not change the state of the program, then removes this code. This brings two benefits at once: reducing the size of the memory occupied by the program and increasing the speed of the program.

Here is a very simple example of Javascript code that is a good candidate for deletion, because the condition will always be false and the js engine can easily delete it.


function func() {
    var x = 1;
    var y = 3;
    var w = x + y;

    if (w != 4) {
        // dead code 
    }
}


«» , , . , ( computer science «» ),


function func(a, b) {
    var x;
    var i = 300;
    while (i--) {
        x = a + b; // dead store
    }
}


, , , , .


function sum() {
    var a = [1, 2, 3, 4, 5];
    var sum = 0.0;
    
    // dead loop elimination
    for (var i = 0; i < 5; i++) {
        sum += a[i];
    }
}


«» , , , . .

, math-cordic Webkit SunSpider suite? .


function cordicsincos() { 
     var X;  
     var Y;  
     var TargetAngle; 
     var CurrAngle;  
     var Step;   
     X = FIXED(AG_CONST);         /* AG_CONST * cos(0) */ 
     Y = 0;                       /* AG_CONST * sin(0) */ 
   
    TargetAngle = FIXED(28.027);  
    CurrAngle = 0;  
    for (Step = 0; Step < 12; Step++) { 
        var NewX; 
            if (TargetAngle > CurrAngle) { 
               NewX = X - (Y >> Step);  
               Y = (X >> Step) + Y; 
               X = NewX; 
               CurrAngle += Angles[Step];  
            } else { 
               NewX = X + (Y >> Step); 
               Y = -(X >> Step) + Y; 
               X = NewX; 
               CurrAngle -= Angles[Step]; 
            } 
    } 
} 


, . , .

, , , .

, . Sunspider . , JavaScript.

JavaScript, «» . , - , -. Chakra «» . , Connect , . «» .

«» , Chakra . JavaScript .

— Dean Hachamovitch

: IE9 ietestdrive.com
:image
UPD: , . .
Detailed ResultsAverage (ms)
IE83746
Firefox 3.6.12753
Safari 5.0.2328
Firefox 4.0 Pre-Release Beta7277
Chrome 7.0262
Opera 10.63246
Opera 11 Alpha242
Chrome 8.0 Beta233
IE9 Platform Preview #7216

Source: https://habr.com/ru/post/108355/


All Articles