Opened 11 years ago

Closed 10 years ago

#7053 closed enhancement (wontfix)

Use new Function() instead of eval() for dojo.fromJson (and possibly other eval situations)

Reported by: kriszyp Owned by: Kris Zyp
Priority: high Milestone: future
Component: General Version: 1.1.1
Keywords: JSON Cc: Adam Peller
Blocked By: Blocking:

Description (last modified by kriszyp)

Recently, I did some tests, and it appears that parsing JSON with: return new Function("return " + json)(); rather than: return eval("(" + json + ")"); is about twice as fast in FF (which has by far the slowest eval performance). It seems that it is also about 20% faster in IE (although IE is already way faster than FF with eval). See any problems with doing this?

Change History (10)

comment:1 Changed 11 years ago by kriszyp

Description: modified (diff)

comment:2 Changed 11 years ago by James Burke

Is this true for all sizes of JSON data? Just wondering if there is a size cutoff where the new Function() call introduces overhead that makes it slower than straight eval.

Also, how about Safari and Opera?

comment:3 Changed 11 years ago by kriszyp

You know I haven't tested this extensively yet, the ticket is definitely still at "research" stage right now :). I think new Function looked a little bit slower on Safari, however both eval and new Function are orders of magnitude faster in Safari than FF. I will test it with larger JSON blocks and get back to you.

comment:4 Changed 11 years ago by Kris Zyp

Milestone: 1.21.3

comment:5 Changed 11 years ago by James Burke

As mentioned in bug #7290, could this be a result of the Firefox 3 regression bug mentioned in that ticket? If so, I am more inclined to punt this as a browser issue that should be fixed once they fix the regression.

comment:6 Changed 11 years ago by Kris Zyp

This issue is almost entirely due to Firebug, not Firefox. I think there might be a slight regression in eval in FF, but it is very minor. One of the major features of Firebug 1.2 (FF3 version) is the ability to debug eval, which means all evals must be processed by Firebug and added to a list of scripts, which is understandably expensive.

comment:7 Changed 10 years ago by dante

Owner: changed from kriszyp to Kris Zyp

comment:8 Changed 10 years ago by Kris Zyp

Milestone: 1.31.4

comment:9 Changed 10 years ago by bill

My tests showed the opposite, that Function was 2x slower on FF, see #9664 (and also, mentioned there).

comment:10 Changed 10 years ago by bill

Milestone: 1.4future
Resolution: wontfix
Status: newclosed

Sounds like this is a "wontfix", at least for now. Feel free to reopen if there's more evidence that Function() is faster in general.

Note: See TracTickets for help on using tickets.