Ok, the reason eval is so slow is because of FireBug, it costed 17 seconds
with FireBug enabled while FireBug seems to do not affect Function execution
(which makes Function against better than eval).

In any case, Function is faster or exactly fast as eval is, even in Internet
Explorer, at least in my tests.

this is a truly simple readapted test case:

<!DOCTYPE html>
<script type="text/javascript">
portable = (function(msg, i, interval){
    function timeout(){
        alert(msg.join("\n"));
        msg = [];
        i = 0;
    };
    return {
        log:function(test){
            msg[i++] = test;
            clearTimeout(interval);
            interval = setTimeout(timeout, 100);
        }
    }
})([], 0, 0);
var count = 10000, o = null, i = 0, jsonString =
'{"value":{"items":[{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3}]},"error":null}';
var beginTime = new Date();
for ( i = 0; i < count; i++ )
    o = eval( "(" + jsonString + ")" );
portable.log( "eval:" + ( new Date() - beginTime ) );
var beginTime = new Date();
for ( i = 0; i < count; i++ )
    o = new Function( "return " + jsonString )();
portable.log( "new Function:" + ( new Date() - beginTime ) );
var beginTime = new Date();
for ( i = 0; i < count; i++ )
    o = Function( "return " + jsonString )();
portable.log( "Function:" + ( new Date() - beginTime ) );
var beginTime = new Date();
var callback = Function( "return " + jsonString );
for ( i = 0; i < count; i++ )
    o = callback();
portable.log( "precompiled Function:" + ( new Date() - beginTime ) );
if(this.JSON){
    var beginTime = new Date();
    for ( i = 0; i < count; i++ )
        o = JSON.parse(jsonString);
    portable.log( "native:" + ( new Date() - beginTime ) );
};
</script>

Let me know what you think.

Regards




On Wed, May 20, 2009 at 7:43 AM, Andrea Giammarchi <
[email protected]> wrote:

> As I said, Function does not do scope resolution except the global one so
> in my opinion should be always preferred for JSON evaluations. Firefox does
> not care about implicit returned value plus brackets plus scope, it simply
> create the anonimous function doing a syntax check and returns errors only
> at call time so if there are no errors the precompiled funcion will be like
> a manual one, time for parsing syntax is then one against every eval call.
> To be fair, that bench make sense declaring the function once and simply
> calling it inside the for to always return the same object. Unfortunately
> this is not a real world case.
>
> On May 20, 2009 7:33 AM, "Michael Geary" <[email protected]> wrote:
>
>  Function( 'return ' + data )() is also MUCH faster in Firefox than eval.
>
> In a test case of JSON data containing 1000 names and addresses (about
> 112KB), eval() takes a full second to execute on my machine in Firefox 3. On
> all other browsers (including IE!) it takes hardly any time at all.
>
> The Function version takes essentially no time in Firefox. (Didn't test it
> in other browsers.)
>
> -Mike
>
>  ------------------------------
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *John Resig
> *Sent:* Tuesday, May 19, 2009 4:20 PM
> *To:* [email protected]
> *Subject:* [jquery-dev] Re: window['eval']() in rhino
>
> > I don't remember the original discussion/change, off-hand. If YUIMin is
> still able to generate an ...
>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to