473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

eval and mutireplace, which is faster?

I want to implement the template for a object output.
I have two methods as follow:

1: Use "eval" to replace the template content
<script>
//<![CDATA[
s = "'<a href=\"' + o.url + '\">' + o.title + '</a>' + o.content + o.v1
+ o.v2 + o.v3 + o.v4 + '</br>'";

o = {url:"http://www.eemap.org",
title: "EEmap",
content: "Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.",
v1: "12345",
v2: "abcde",
v3: "edfgt",
v4: "gtyh"
};
output = '';

startTime = new Date();
for(i=0; i<500; i++)
{
t = eval(s);
output += t;
}
endTime = new Date();
document.write(endTime + '-' + startTime + '=' + (endTime -
startTime));
document.write('<br />');
document.write(output);
//]]>
</script>

2: use many "replace" to replace the template content:
<script>
//<![CDATA[
s = "<a href='%url%'>%title%</a>%content%%v1%%v2%%v3%%v4%</br>";
o = {url:"http://www.eemap.org",
title: "EEmap",
content: "Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.",
v1: "12345",
v2: "abcde",
v3: "edfgt",
v4: "gtyh"
};
output = '';

startTime = new Date();
for(i=0; i<500; i++)
{
s = s.replace("%title%", o.title);
s = s.replace("%content%", o.content);
s = s.replace("%v1%", o.v1);
s = s.replace("%v2%", o.v2);
s = s.replace("%v3%", o.v3);
s = s.replace("%v4%", o.v4);
output += s;
}
endTime = new Date();
document.write(endTime + '-' + startTime + '=' + (endTime -
startTime));
document.write('<br />');
document.write(output);
//]]>
</script>

I test the snap code as follow, and found the snap 2 is faster than
snap 1 both Firefox and IE. but if the muber of o increment, which is
better? And is there a simple code replace with the multi-line replace?

Dec 26 '06 #1
1 1138
rollenc wrote:
I want to implement the template for a object output.
I have two methods as follow:

1: Use "eval" to replace the template content
<script>
//<![CDATA[
s = "'<a href=\"' + o.url + '\">' + o.title + '</a>' + o.content + o.v1
+ o.v2 + o.v3 + o.v4 + '</br>'";

o = {url:"http://www.eemap.org",
title: "EEmap",
content: "Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.",
v1: "12345",
v2: "abcde",
v3: "edfgt",
v4: "gtyh"
};
output = '';

startTime = new Date();
for(i=0; i<500; i++)
{
t = eval(s);
output += t;
}
endTime = new Date();
document.write(endTime + '-' + startTime + '=' + (endTime -
startTime));
document.write('<br />');
document.write(output);
//]]>
</script>

2: use many "replace" to replace the template content:
<script>
//<![CDATA[
s = "<a href='%url%'>%title%</a>%content%%v1%%v2%%v3%%v4%</br>";
o = {url:"http://www.eemap.org",
title: "EEmap",
content: "Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love EEmap.Yes, I love
EEmap.Yes, I love EEmap.Yes, I love EEmap.",
v1: "12345",
v2: "abcde",
v3: "edfgt",
v4: "gtyh"
};
output = '';

startTime = new Date();
for(i=0; i<500; i++)
{
s = s.replace("%title%", o.title);
s = s.replace("%content%", o.content);
s = s.replace("%v1%", o.v1);
s = s.replace("%v2%", o.v2);
s = s.replace("%v3%", o.v3);
s = s.replace("%v4%", o.v4);
output += s;
}
endTime = new Date();
document.write(endTime + '-' + startTime + '=' + (endTime -
startTime));
document.write('<br />');
document.write(output);
//]]>
</script>

I test the snap code as follow, and found the snap 2 is faster than
snap 1 both Firefox and IE. but if the muber of o increment, which is
better? And is there a simple code replace with the multi-line replace?
I don't know which is better of the above but there are other
preexisting templating systems for use with JavaScript. For example,

<URL: http://erik.eae.net/archives/2005/05/27/01.03.26/>
<URL: http://trimpath.com/project/wiki/JavaScriptTemplates>

Peter

Dec 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Leif K-Brooks | last post by:
In Python 2.4, although None can't be directly assigned to, globals() can still be; however, that won't change the value of the expression "None" in ordinary statements. Except with the eval...
9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
33
by: Stuart | last post by:
why won't the following work for(var i=0;i<pics;i++){ eval('img'+i) = new Image(wth,hgt) eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif' } basically I am trying to create a numer of...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
9
by: Jim Washington | last post by:
I'm still working on yet another parser for JSON (http://json.org). It's called minjson, and it's tolerant on input, strict on output, and pretty fast. The only problem is, it uses eval(). It's...
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
4
by: spamme | last post by:
Hi, As you can see from the following code, I am trying to manipulate a large number of layers. I have read many times in the past how the "Eval" function should be used very sparingly as it can...
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.