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

Is it about providing -this- value?

AKS
Hi! Here's a small test case (for Firefox):

var o = {
p: {
func: function () {},
},

test: function () {
var msg = [];
var d = new Date;
var i = 1000000;
var f = this.p.func;
while (i--) {
f();
};
msg[0] = (new Date) - d; // ~ 2700 ms
d = new Date;
i = 1000000;
while (i--) {
this.p.func();
};
msg[1] = (new Date) - d; // ~ 900 ms
console.log(msg.join('\n'));
}
};

o.test();
The first iteration takes more time than the second one. But the
second one performs property access (two dot operators)! Can someone
shed some light on this behavior?
Here's my thoughts:
When this value is null then null must be replaced with a global
value. And search for the global value (by the JavaScript engine)
takes too much time.
Apr 3 '08 #1
8 1432
AKS <ak******@yandex.ruwrites:
Hi! Here's a small test case (for Firefox):

var o = {
p: {
func: function () {},
},

test: function () {
var msg = [];
var d = new Date;
var i = 1000000;
var f = this.p.func;
while (i--) {
f();
};
msg[0] = (new Date) - d; // ~ 2700 ms
d = new Date;
i = 1000000;
while (i--) {
this.p.func();
};
msg[1] = (new Date) - d; // ~ 900 ms
console.log(msg.join('\n'));
}
};

o.test();
The first iteration takes more time than the second one. But the
second one performs property access (two dot operators)! Can someone
shed some light on this behavior?
In the spidermonkey 1.7.0 shell the first call is a fraction faster
than the second. (175 vs 195 ms), so the speed differences appear to be
unpredictable.
Here's my thoughts:
When this value is null then null must be replaced with a global
value. And search for the global value (by the JavaScript engine)
takes too much time.
What? No. There is only one global variable, so there isn't any need to
search for it. Besides, you're not using this in your call. Or maybe
you're just confusing me.

If you're worried about speed, fix your code so that you don't have
to do a million function calls, all at once. You appear to be micro
optimizing and I don't see any reason for it.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Apr 3 '08 #2
AKS
On Apr 3, 3:24*pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
In the spidermonkey 1.7.0 shell the first call is a fraction faster
than the second. (175 vs 195 ms).
Yes, it is true for old versions. But I've tested it in the latest
versions of FireFox (1.5+), and my results (2700 vs 900) are true just
for them.
There is only one global variable
Thank you, Joost! I know it.
so there isn't any need to search for it.
Are you sure that there's no computation of global value? After I've
looked at this code (http://lxr.mozilla.org/seamonkey/source/js/src/
jsinterp.c#738), I had doubts.

If you're worried about speed, fix your code so that you don't have
to do a million function calls, all at once.
It's not production code. It should be considered as an example for
the educational purposes.

Apr 3 '08 #3
On Apr 3, 3:47*pm, AKS <aksus...@yandex.ruwrote:
Hi! Here's a small test case (for Firefox):

var o = {
* * p: {
* * * * func: function () {},
* * },

* * test: function () {
* * * * var msg = [];
* * * * var d = new Date;
* * * * var i = 1000000;
* * * * var f = this.p.func;
* * * * while (i--) {
* * * * * * f();
* * * * };
* * * * msg[0] = (new Date) - d; // ~ 2700 ms
* * * * d = new Date;
* * * * i = 1000000;
* * * * while (i--) {
* * * * * * this.p.func();
* * * * };
* * * * msg[1] = (new Date) - d; // ~ 900 ms
* * * * console.log(msg.join('\n'));
* * }

};

o.test();

The first iteration takes more time than the second one.
Not in Safari 3.1 Mac, the first is faster (1060ms vs 1220ms), also in
Firefox 3.0b4 Mac (390ms vs 470ms) on a fairly old iBook.

But the
second one performs property access (two dot operators)! Can someone
shed some light on this behavior?
Not me, other than to say you should test widely before proposing
theories about behaviour or why it occurs. What seems logical from
one browser may not be borne out when tested in others.
Here's my thoughts:
When this value is null then null must be replaced with a global
value. And search for the global value (by the JavaScript engine)
takes too much time.
You can guess all you like, but better to ask in a forum that knows
the guts of Firefox.
--
Rob
Apr 3 '08 #4
AKS
On Apr 3, 6:00 pm, RobG <rg...@iinet.net.auwrote:
What seems logical from
one browser may not be borne out when tested in others.
I've tested this code in FF 1.5, FF 2.0, FF 3b2 (WinXp).

but better to ask in a forum that knows
the guts of Firefox.
Thanks, I'll try to do it.

Apr 3 '08 #5
AKS <ak******@yandex.ruwrites:
Are you sure that there's no computation of global value? After I've
looked at this code (http://lxr.mozilla.org/seamonkey/source/js/src/
jsinterp.c#738), I had doubts.
<thisisn't scoped like other variables. it's always directly set to
either the global object, or a new object or whatever object is the
reciever of the message/method call. And in case of the global object,
that *can* be determined at parse/compile time.

Not that that code does anything like that (if I read it correctly), but
in any case, that code will probably (handwave, cough, cough) take about
as much time (or less) per nested scope as resolving each object in a
thing.that.such.bla() call would.
>If you're worried about speed, fix your code so that you don't have
to do a million function calls, all at once.

It's not production code. It should be considered as an example for
the educational purposes.
What would be far more interesting from a practical POV is how much
overhead a method/function call adds when you're actually doing some
reasonable amount of work in the call.

Check the times on this example (not that counting to 400 is such a good
test, but it's just to give an indication):

if (!console) console = { log: print }; // for spidermonkey shell

var o = {
p: {
func: function () { for (var k = 0; k < 400; k++) { } },
},

test: function () {
var msg = [];
var d = new Date;
var i = 10000;
var f = this.p.func;
while (i--) {
f();
};
msg[0] = (new Date) - d; // ~ 170 ms
d = new Date;
i = 10000;
while (i--) {
this.p.func();
};
msg[1] = (new Date) - d; // ~ 170 ms
d = new Date;
i = 10000;
while (i--) {
for (var k = 0; k < 400; k++) {
;
}
};
msg[2] = (new Date) - d; // ~ 169 ms
console.log(msg.join('\n'));
}
};

o.test();
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Apr 3 '08 #6
AKS


Joost Diepenmaat wrote:
<thisisn't scoped like other variables. it's always directly set to
either the global object, or a new object or whatever object is the
reciever of the message/method call. And in case of the global object,
that *can* be determined at parse/compile time.
seamonkey/ js/ src/ jsinterp.c (wrote in comments):

- ECMA requires "the global object", but in embeddings such as the
browser, which have multiple top-level objects (windows, frames, etc.
in the DOM), we prefer fun's parent.

Take a look at -js_ComputeGlobalThis- algorithm.
Not that that code does anything like that (if I read it correctly)
Every time when f() is called it receives null as this value. Then
null must be replaced with global value (regardless of whether it will
be used or not).

Check the times on this example (not that counting to 400 is such a good
test, but it's just to give an indication):
My expectation is: caching property access into a local variable must
optimize my code.
Apr 3 '08 #7
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
>AKS <ak******@yandex.ruwrites:
>>Are you sure that there's no computation of global value? After I've
looked at this code (http://lxr.mozilla.org/seamonkey/source/js/src/
jsinterp.c#738), I had doubts.

<thisisn't scoped like other variables.

More, `this' is _not_ a variable.
True enough, but I think we're splitting hairs, here.
>[Code that uses (new Date()) with console.log() to determine the run time]

Do you know that Firebug has a built-in profiler?
The question considers the differences in performance between different
function/method calls (and no function call at all, which firebug's
profiler doesn't profile). For finding performance bottlenecks in real
case scenarios, firebug's is usually a much better choice, but it's not
always the right tool for benchmarks.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Apr 3 '08 #8
On Apr 4, 9:52 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Thomas 'PointedEars' Lahn <PointedE...@web.dewrites:
[...]
Do you know that Firebug has a built-in profiler?

The question considers the differences in performance between different
function/method calls (and no function call at all, which firebug's
profiler doesn't profile). For finding performance bottlenecks in real
case scenarios, firebug's is usually a much better choice, but it's not
always the right tool for benchmarks.
Also, Firebug isn't available for the latest Firefox betas, nor other
browsers so for cross-browser testing, it's much easier to use a
Date() and either write the results to the page or use an alert.
--
Rob
Apr 4 '08 #9

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
4
by: Benny | last post by:
Dear All, I am performing a data import on the SQL server. Due to fact that I use the excel file as a source. Some of cells in excel are actually empty, they become NULL fields after importing...
10
by: ma740988 | last post by:
I'm trying to unpack a 32 bit word into 3-10 bits samples. So now: Sample0 corresponds to bits 0-9 Sample1 corresponds to bits 10-19 Sample2 corresponds to bits 20-29 Bits 30 and 31 are don't...
0
by: Lee | last post by:
Once the business logic has been written and the basic elements are built into a page, what is the recommended way to provide the site innards to graphics design people without providing source...
8
by: Tom | last post by:
I was reading Bjourne's book and wonder about constructors? If I have a class template<class T> class Vector { public: explicit Vector(size_t n); } Does a default constructor get...
3
by: goodmen | last post by:
I think the boost::concept_check is too complex. Here is my solution about static interface and concept. The static interface can be used as function param. It is just a proxy of the real...
3
by: Salad | last post by:
With the database window open I click on Help, then click on "About Microsoft Office Access". A dialog window is presented and there are 4 buttons at the bottom right of the form. One is...
0
by: watches0898 | last post by:
Edwards Garments Company is one popular industry specific designer specializing in work wear that ranges from chef coats to chef hats to separates to housecleaning uniforms. Edwards Garments...
10
by: =?Utf-8?B?RGFuaQ==?= | last post by:
Hi, Trying to create a master page that holds a menu, and the menu switches between pages in the site. 2 problem arrosed: a. When I navigate from page to page (all AJAX Web Forms, with the...
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:
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...
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
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.