473,804 Members | 3,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1452
AKS <ak******@yande x.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.n lwrote:
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...@yande x.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.ne t.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******@yande x.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_ComputeGloba lThis- 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*********@we b.dewrites:
Joost Diepenmaat wrote:
>AKS <ak******@yande x.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#73 8), 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.n lwrote:
Thomas 'PointedEars' Lahn <PointedE...@we b.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
19190
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
12
2768
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 in modern day applications. Should we readily use it when we can or only when absolutly forced to use it?
4
9398
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 into the SQL server. Actually I want these fields are empty string instead of NULL. Does SQL server has any approach to make these fields to be empty string instead of NULL when importing?? Or is there any store procedure exist for converting the...
10
1647
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 cares So my unpacker looks like: void unpack(
0
978
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 code too? Thanks for advice. -- Warm Regards, Lee
8
1659
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 generated by the compiler?
3
1703
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 implementation. And the Concept can be used when building the class hierarchy. It is almost a private inherit, but for the sake of providing an uniform look with the static interface version, the private member is used.
3
2103
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 SystemInfo and when I press that I get a Summary dialog box of particulars. One of the options is Office 2003 Applications. If I press on the plus sign O2003Apps, I can drill down and select Access 2003 and drill down to its selections. In the...
0
1337
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 Company has a reputation for providing a high quality, affordable solution to the professional man's or woman's wardrobe. Often, white is the color given most focus, but a variety of colors are offered to wet your appetite, so to speak. ...
10
2149
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 Master pages as their master...) the entire page is refreshed - also the menu which belongs to the master, how can I fix it - so only the inside content will be refreshed ?
0
9582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.