Performance of Google chrome vs nodejs (v8)?

后端 未结 3 716
暖寄归人
暖寄归人 2021-02-01 08:42

\"enter

Example

     console.time("Test");
     for(var i=0; i         


        
3条回答
  •  星月不相逢
    2021-02-01 09:38

    Its Due To SCOPE in JavaScript

    In Browser console code without scope : Takes lots of time : Test: 1154.19ms

    below code will be kept in heavily populated window object which will take time to resolve;

     console.time("Test");
     for(var i=0; i <2500000; i +=1 ){
             // loop around
     }
     console.timeEnd("Test");
    

    In Browser console code with scope : Takes less time Test: 3.06ms

    below code will be kept inside JavaScript scope and scope will be almost empty so less time

    function rocket(){
        console.time("Test");
         for(var i=0; i <2500000; i +=1 ){
                 // loop around
         }
         console.timeEnd("Test");
    }
    rocket()
    

    In Nodejs REPL : code without scope : Test: 14ms

    Unexpected But nodejs most outer scope contains some bunch of variables

     console.time("Test");
     for(var i=0; i <2500000; i +=1 ){
             // loop around
     }
     console.timeEnd("Test");
    

    In Nodejs REPL : code with scope : Test: 2ms

    below code will be kept inside JavaScript scope and scope will be almost empty so less time

    function rocket(){
        console.time("Test");
         for(var i=0; i <2500000; i +=1 ){
                 // loop around
         }
         console.timeEnd("Test");
    }
    rocket()
    

    Conclusion : Its all due to SCOPE and SCOPING in JavaScript is done by functions

    Means if you create new function ,the place inside curly brackets {} is called as SCOPE and it will be empty initially, and if you create variable in this scope it will take no time to execute

提交回复
热议问题