By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Anonymous function increases speed

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.