A faster alternative to eval()

UPDATE: A lot of people are hitting this page looking for ways to parse a server response – usually this involves converting a JSON string to a Javascript Object. For those purposes you should use the JSON.parse, it’s supported natively in the following browsers:

  • Microsoft Internet Explorer 8+
  • Mozilla Firefox 3.5+
  • Webkit-based browsers (e.g. Google Chrome, Apple Safari)
  • Opera 10.5+


If you need to support older browsers like IE6 or 7, you should include Crockfords JSON2 library, it will create the JSON property if there isn’t already one.

The rest of this post is for when you really need to evaluate an expression in Javascript.

The problem with eval() in JavaScript (well one of them) is that it’s quite expensive performance wise, so here’s a little trick I first saw over at paulirish.com.

I use it in AJAX whenever I need to convert a json string to an object, usually when getting a response from a webservice. With a regular eval() this could look something like this.

// A very basic callback with a result from the webserver
success: function( result ) {
	// Evaluate the json string to create the object literal
	var res = eval( result );

	//... and on to other stuff
}

But instead of using eval() you can create a new function and immediately invoke it to return the object. The same example will then look like this:

// A very basic callback with a result from the webserver
success: function( result ) {
	// Create a new function and immediately invoke it to create the object literal
	var res = ( new Function( 'return ' + result ) )();

	//... and on to other stuff
}

And there you have it, same result, less hassle. Here’s a fiddle for you to play around with and see it in action: jsfiddle.net/mordendk/yyc6N/


6 Responses to A faster alternative to eval()

  1. Tom says:

    Also, if your only objective is to parse a JSON object, you can do:
    var obj = JSON.parse( result );

    The other method can be rather dangerous, as it will execute arbitrary javascript commands, that may be nefariously embedded in the result (that is – if you’re working with external datasources)

    • moe says:

      Yeah, if you’re able to use native JSON, surely that’s the best option. As of today i believe the native JSON is implemented in the following mainstream browsers:

      * Microsoft Internet Explorer 8+
      * Mozilla Firefox 3.5+
      * Webkit-based browsers (e.g. Google Chrome, Apple Safari)
      * Opera 10.5+

      Regarding security your absolutely right, there’s a hazard – but that would be the case with eval() too.

      I guess again use native whenever possible. Theres also Crockfords JSON2 library and jQuerys .getJSON() that will use native if available.

  2. Ian says:

    Nice idea but sadly it doesn’t always work. I currently have a piece of code that eval() deals with correctly but the method above causes the result to be undefined.

    It would appear that they’re not always interchangeable.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>