Vasil

Additionally the const of the both approaches is pretty much the same.

The difference is that in first approach you iterate the properties and build the object yourself. And in the second approach you pass some big constructor for this object, and the JS engine parses the values and then the browser do the same: iterate them and build the object.

If you perform some test, you will get very similar times in both large and small set of data. Here is some code based on yours:

var fields = [];

var data2 = [];

for (var j = 0 ; j < 1000; j++) {

fields.push("a" + j + "r" + Math.floor(Math.random() * 100));

}

for (var i = 0; i < 10000 ; i++) {

var item = [];

for (var j = 0 ; j < 100; j++) {

item.push(Math.random());

}

data2.push(item);

}

var data1;

function f1() {

data1 = [];

data1.length = data2.length;

var temp;

for (var i = 0; i < data2.length; i++) {

temp = {};

for (var y = 0; y < fields.length; y++) {

temp[fields[y]] = data2[i][y];

}

data1.push(temp);

}

};

function f2() {

var body = "";

for (var i = 0; i < fields.length; i++) {

body += "this." + fields[i] + "=args[" + i + "]; ";

}

var model = new Function("args", body);

data1 = [];

data1.length = data2.length;

for (var i = 0; i < data2.length; i++) {

var x = new model(data2[i]);

data1.push(x);

}

};

The times that I get are 2.478s for the first function and 2.264 for the second for this very big set of data. For small set of data they work practically the same.

From profiling in chrome it seems that the difference comes mainly from execution of the garbage collector more often in the first function.

So you can use the working approach without any worries. I will additionally debug why the second one fails when passed as a parse function.

Dominik

Thank you for looking into this for me! Technically the first solution is (M*N) and the second solution is (M+N). For simplicity I used (N^2) and (N). But you're right, for small amounts of data there is little to no difference.

Vasil

Interesting is that in Firefox (Nightly Builds) the solution 2 is actually slower than solution 1.

Back to the original subject. I tried to get the same error as you, but in my case it is working correct, even with your original code.
Here is testing example in our dojo:http://dojo.telerik.com/egIsA
When I run it I get the alert displaying the name and age of the first record. And looking at the console, there is no exception like described.

What I see here as the potential problem if the fields contains numbers without literal prefix.
For example if the field is called "123" instead of "name". Then you will get exception when evaluating function like:
"this.123 = something".
however the first solution will work:
this[123] = something

Could you check the example code above, and tell me what is different in your case. You can edit the example and write here the new link you get.

Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.