473,382 Members | 1,657 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,382 software developers and data experts.

Anonymous function increases speed

I found the results of the following code counter-intuitive:

var N = 2000000;
var c1 = 0, c2 = 0, t1, t2;

t1 = new Date();
(function () {
for (var i = 0; i < N; ++i) {
c1++;
}
})();
t2 = new Date();
var elapsed1 = t2.getTime() - t1.getTime();

t1 = new Date();
for (var i = 0; i < N; ++i) {
c2++;
}
t2 = new Date();
var elapsed2 = (t2.getTime() - t1.getTime());

alert('elapsed1 = ' + elapsed1 + ', elapsed2 = ' + elapsed2);
alert('c1 = ' + c1 + ', c2 = ' + c2);

I'm running Firefox 2.0.0.6 on an old laptop running Ubuntu Linux.

I get elapsed1=2,041 and elapsed2=3,477, so it appears that wrapping a
for loop in an anonymous function makes it run faster.

If I declare i with a var statement in the same decl as c1, etc. and
remove 'var' from both for loops, then I get elapsed1=3,507 and
elapsed2=3,452, so that removes the speed advantage of the anonymous
function.

If I leave the code as above, but wrap the entire thing in an
anonymous function that is executed, then I get elapsed1=1,490 and
elapsed2=423 !

I doubt the lesson here is to wrap code in anonymous functions to gain
speed, so what am I missing here?

Brian Adkins

Nov 1 '07 #1
3 1813
Ed
Brian Adkins wrote:
I found the results of the following code counter-intuitive:

var N = 2000000;
var c1 = 0, c2 = 0, t1, t2;

t1 = new Date();
(function () {
for (var i = 0; i < N; ++i) {
c1++;
}
})();
t2 = new Date();
var elapsed1 = t2.getTime() - t1.getTime();

t1 = new Date();
for (var i = 0; i < N; ++i) {
c2++;
}
t2 = new Date();
var elapsed2 = (t2.getTime() - t1.getTime());

alert('elapsed1 = ' + elapsed1 + ', elapsed2 = ' + elapsed2);
alert('c1 = ' + c1 + ', c2 = ' + c2);

I'm running Firefox 2.0.0.6 on an old laptop running Ubuntu Linux.

I get elapsed1=2,041 and elapsed2=3,477, so it appears that wrapping a
for loop in an anonymous function makes it run faster.

If I declare i with a var statement in the same decl as c1, etc. and
remove 'var' from both for loops, then I get elapsed1=3,507 and
elapsed2=3,452, so that removes the speed advantage of the anonymous
function.

If I leave the code as above, but wrap the entire thing in an
anonymous function that is executed, then I get elapsed1=1,490 and
elapsed2=423 !

I doubt the lesson here is to wrap code in anonymous functions to gain
speed, so what am I missing here?
I think the lesson is "Avoid using global variables":

http://dev.opera.com/articles/view/e...=2#avoidglobal

Nov 1 '07 #2
On Nov 1, 1:10 pm, Ed <e...@siliconforks.comwrote:
Brian Adkins wrote:
I doubt the lesson here is to wrap code in anonymous functions to gain
speed, so what am I missing here?

I think the lesson is "Avoid using global variables":

http://dev.opera.com/articles/view/e.../?page=2#avoid...
Indeed. Thanks for the link.

I was aware of other reasons to avoid global variables in general,
which weren't important to me for the ad-hoc test case that prompted
this, but I wasn't fully aware of their inefficiencies. In particular,
I found the following info from the article interesting:

"In the global scope, variables are always located using their name,
instead of using an optimized predefined index, as they can be in
local scopes. A global variable will take longer for the script engine
to find, as a result."

What are the disadvantages to changing the following:

<script type="text/javascript">
// top level declarations and code
</script>

to:

<script type="text/javascript">
(function () {
// top level declarations and code
})();
</script>

Brian Adkins

Nov 1 '07 #3
Brian Adkins wrote:
On Nov 1, 1:10 pm, Ed <e...@siliconforks.comwrote:
>Brian Adkins wrote:
>>I doubt the lesson here is to wrap code in anonymous functions
to gain speed, so what am I missing here?

I think the lesson is "Avoid using global variables":
<snip>
Indeed. Thanks for the link.

I was aware of other reasons to avoid global variables in
general, which weren't important to me for the ad-hoc test
case that prompted this, but I wasn't fully aware of their
inefficiencies. In particular, I found the following info
from the article interesting:

"In the global scope, variables are always located using
their name, instead of using an optimized predefined index,
as they can be in local scopes. A global variable will take
longer for the script engine to find, as a result."
You should remember that there are many javascript implementations and
nothing that guarantees that any two will do anything specific in the
same way internally. This statement is probably true of Opera (because
of the origin or that statement) but it is not necessarily true in any
other browser.

When timing things it is usually a good idea to time then in a good
range of browsers before drawing any general conclusions about code
performance.
What are the disadvantages to changing the following:

<script type="text/javascript">
// top level declarations and code
</script>

to:

<script type="text/javascript">
(function () {
// top level declarations and code
})();
</script>
The second means that whatever gets declared is not globally available.
Apart from that, and the slight delay in the creation of whatever is
being declared, there are no significant disadvantages. But this is
really the sort of thing you should only do when you have a positive
reason for doing it, not just the absence of disadvantages.

Richard.

Nov 2 '07 #4

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

Similar topics

1
by: nortelsale | last post by:
i have a site that allow people to post for sale, for rent info, I want to have advanced search function that can search all the posting (in my site) by zip code, and by certain miles say 10, 20...
76
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax,...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
2
by: sybot_uk | last post by:
Hi there, I am writing a query in DB2 which needs to return a result set, i want to be able to use a couple of variables in query to speed things up. Please can anyone tell me how i may do this?...
2
by: WGW | last post by:
Hello all, I need another set of eyes cause it just isn't working, no matter how identical I make it. The initRotator function works when called by itself, but when adding another function, only the...
1
by: Paul Childs | last post by:
Hi folks, I'll start off with the code I wrote... (ActivePython 2.4 on Windows XP SP2) ------------------------------- class FlightCondition(object): lsf = vto =
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.