-2

I am running following code on Chrome console and getting different execution time.

I create two functions with same body. first one is function declaration and second one is function expression.

var t= new Date().getTime();
function fun1(){  
    for( i=0; i < 1000000; i ++) 
    {
        a=i;
     };
}; 
fun1();
console.log(new Date().getTime() - t);  // nearly 2ms;
t= new Date().getTime();
var fun2 = function (){  
    for(i=0; i < 1000000; i ++) 
    {
       a=i;
    };
 }; 
 fun2();
console.log(new Date().getTime() - t); //nearly 900ms

Why is different behavior for same loop?

Anoop
  • 23,044
  • 10
  • 62
  • 76
  • 3
    I got 680 and 707. There doesn't seem to be an issue here. – Patrick Q Jul 30 '14 at 14:00
  • My chrome version: Version 37.0.2062.44 beta-m OS: windows 8.1 – Anoop Jul 30 '14 at 14:01
  • The drive-by downvoters and closevoters don't get the issue at hand because your question is far too concise (and not posed a real question). The issue at hand is actually quite interesting - I get 3 and 1100 respectively here in 37-beta, but have no clue why a single closure invocation would add this much overhead. Very curious about the answer. I'd recommend deleting it and rewriting as a real question, explaining what is happening. – Niels Keurentjes Jul 30 '14 at 14:01
  • Where is the question? – Ash Burlaczenko Jul 30 '14 at 14:01
  • @NielsKeurentjes How is posting a contrary result a "dumb comment"? – Patrick Q Jul 30 '14 at 14:03
  • @PatrickQ wasn't directed at you but at 'undefined' who posted a silly 'thanks for sharing' right before you - he deleted it already. Are you running 36-stable with the conflicting result? – Niels Keurentjes Jul 30 '14 at 14:04
  • I'm seeing a dramatic difference too. Dunno why, though I suspect the answer is something along the lines of one function happening to trigger the JIT and the other getting interpreted. – user2357112 Jul 30 '14 at 14:06
  • Annop, people don't respond fondly to no-effort questions. Just pasting code here and saying "explain this" will get you downvotes and close votes. – Anonymous Jul 30 '14 at 14:09
  • i love how someone faved this at all those downvotes :D actually, its not downvoteworthy at all – Alex Jul 30 '14 at 14:09
  • maybe its a cache thing? – Alex Jul 30 '14 at 14:09
  • What happens if you declare `i` properly in the loop, and `a` for that matter? – Andy Jul 30 '14 at 14:10
  • i get pretty much ~1336 for the second log every time, using latest chrome on mac – Alex Jul 30 '14 at 14:10
  • if you declare i and a in side function then time is same. – Anoop Jul 30 '14 at 14:11
  • I'll admit that my initial results were from Firefox, not Chrome. So perhaps it was a "dumb comment" after all :) Seeing similar results in Chrome and Safari (similar to OP), so it appears to be an engine-specific implementation issue. – Patrick Q Jul 30 '14 at 14:11
  • 1
    This just happens by executing the code on the console, the result is the same for me on Chrome 36, http://jsfiddle.net/Qkqb6/ – Ram Jul 30 '14 at 14:14
  • btw http://jsperf.com/samel0o0p – Alex Jul 30 '14 at 14:20

1 Answers1

0

Console code is wrapped in with statement in Google Chrome.

function fun1() { 
    // code
}

fun1 is declaration and it is running outside with block

var fun2 = function () {
    //code
}

fun2 is expression and running inside unoptimizable under V8 with block

both expression and declaration have same perfomance in "non-console" mode :)

Andrei Lesnitsky
  • 1,038
  • 7
  • 14