Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24817 > unrolled thread
| Started by | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| First post | 2014-06-13 16:32 +0200 |
| Last post | 2014-06-18 21:04 +0200 |
| Articles | 11 — 3 participants |
Back to article view | Back to comp.lang.javascript
Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-13 16:32 +0200
Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-13 16:56 +0200
Re: Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-13 17:21 +0200
Re: Bind custom function to the all Function objects "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-13 08:39 -0700
Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-13 19:01 +0200
Re: Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-13 22:41 +0200
Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-14 01:57 +0200
Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-14 15:55 +0200
Re: Bind custom function to the all Function objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-06-16 21:55 +0200
Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-16 23:26 +0200
Re: Bind custom function to the all Function objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-18 21:04 +0200
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2014-06-13 16:32 +0200 |
| Subject | Bind custom function to the all Function objects |
| Message-ID | <98e91$539b0b89$6def49ce$21580@nntpswitch.blueworldhosting.com> |
I am looking for a solution where I can bind custom function to the all Function objects. Whenever any function/method will be called I'd like to call my custom function. I am going to use it only for testing/debugging. Doesn't have to be cross-browser. Can someone suggest from where to start? -- Cezary Tomczyk http://www.ctomczyk.pl/
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-13 16:56 +0200 |
| Message-ID | <1649322.pjBWu2vbqe@PointedEars.de> |
| In reply to | #24817 |
Cezary Tomczyk wrote:
> I am looking for a solution where I can bind custom function to the all
> Function objects. Whenever any function/method will be called I'd like
> to call my custom function. I am going to use it only for
> testing/debugging. Doesn't have to be cross-browser.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What does this mean?
> Can someone suggest from where to start?
No, because it is not possible.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2014-06-13 17:21 +0200 |
| Message-ID | <94a15$539b16e4$6def49ce$21821@nntpswitch.blueworldhosting.com> |
| In reply to | #24818 |
2014-06-13 16:56, Thomas 'PointedEars' Lahn wrote: > Cezary Tomczyk wrote: > >> I am looking for a solution where I can bind custom function to the all >> Function objects. Whenever any function/method will be called I'd like >> to call my custom function. I am going to use it only for >> testing/debugging. Doesn't have to be cross-browser. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > What does this mean? This means that if one browser support required functionality then I'll use that specified browser in my case. >> Can someone suggest from where to start? > > No, because it is not possible. What a pity. -- Cezary Tomczyk http://www.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2014-06-13 08:39 -0700 |
| Message-ID | <576fdad7-76a6-479f-b4de-90d3cc8e56dd@googlegroups.com> |
| In reply to | #24819 |
"On Friday, June 13, 2014 9:32:37 AM UTC-5, Cezary Tomczyk wrote:
> I am looking for a solution where I can bind custom function to the all
> Function objects. Whenever any function/method will be called I'd like
> to call my custom function. I am going to use it only for
> testing/debugging. Doesn't have to be cross-browser.
>
> Can someone suggest from where to start?
Potentially with Proxies, but you'll have to do a bit of manual work:
---
function foo(){
alert("Foo")
}
var bar = Proxy(foo,{
apply: function(target,thisArg,args){
alert("Proxy was called");
target.apply(thisArg,args);
}
})
bar()
---
more: <http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies>"
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-13 19:01 +0200 |
| Message-ID | <2459085.3y454gxjXg@PointedEars.de> |
| In reply to | #24819 |
Cezary Tomczyk wrote: > 2014-06-13 16:56, Thomas 'PointedEars' Lahn wrote: >> Cezary Tomczyk wrote: >>> I am looking for a solution where I can bind custom function to the all >>> Function objects. Whenever any function/method will be called I'd like >>> to call my custom function. I am going to use it only for >>> testing/debugging. Doesn't have to be cross-browser. >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> What does this mean? > > This means that if one browser support required functionality then I'll > use that specified browser in my case. Learn to think in terms of versions of implementations, not browsers. >>> Can someone suggest from where to start? >> No, because it is not possible. > > What a pity. <http://www.catb.org/~esr/faqs/smart-questions.html#goal> -- PointedEars FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix> Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2014-06-13 22:41 +0200 |
| Message-ID | <1e6b6$539b6200$6def49ce$25031@nntpswitch.blueworldhosting.com> |
| In reply to | #24821 |
2014-06-13 19:01, Thomas 'PointedEars' Lahn wrote: > Cezary Tomczyk wrote: > >> 2014-06-13 16:56, Thomas 'PointedEars' Lahn wrote: >>> Cezary Tomczyk wrote: >>>> I am looking for a solution where I can bind custom function to the all >>>> Function objects. Whenever any function/method will be called I'd like >>>> to call my custom function. I am going to use it only for >>>> testing/debugging. Doesn't have to be cross-browser. >>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >>> What does this mean? >> >> This means that if one browser support required functionality then I'll >> use that specified browser in my case. > > Learn to think in terms of versions of implementations, not browsers. Agreed. However before I start think in terms of versions of implementations I need to figure out those implementations. Based on your previous answer there is nothing I can find and use in my case (below details). >>>> Can someone suggest from where to start? >>> No, because it is not possible. >> >> What a pity. > > <http://www.catb.org/~esr/faqs/smart-questions.html#goal> Let me rephrase it. When I run the web application in the browser environment then I'd like to check how many times every functions/methods are called. The simplest way is to add console.count (https://developer.mozilla.org/en-US/docs/Web/API/console.count) to every function/method. However, I think that in reality it's not possible to do that because then changes needs to be done everywhere in the code. So, I thought that it would be better to "wrap" every function/method globally (not sure if I described it correctly) so that every call of function/method in web application first will call custom function before and put information's from console.count. In that way I could measure how many times every function/method is called. That's my goal. -- Cezary Tomczyk http://www.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-14 01:57 +0200 |
| Message-ID | <1464959.oZ30Yp1K6V@PointedEars.de> |
| In reply to | #24829 |
Cezary Tomczyk wrote:
> 2014-06-13 19:01, Thomas 'PointedEars' Lahn wrote:
>> Cezary Tomczyk wrote:
>>> 2014-06-13 16:56, Thomas 'PointedEars' Lahn wrote:
>>>> Cezary Tomczyk wrote:
>>>>> I am looking for a solution where I can bind custom function to the
>>>>> all Function objects. Whenever any function/method will be called I'd
>>>>> like to call my custom function. I am going to use it only for
>>>>> testing/debugging. Doesn't have to be cross-browser.
>>>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> What does this mean?
>>>
>>> This means that if one browser support required functionality then I'll
>>> use that specified browser in my case.
>>
>> Learn to think in terms of versions of implementations, not browsers.
>
> Agreed. However before I start think in terms of versions of
> implementations I need to figure out those implementations
So you should ask “Which implementation X supports feature Y?” and then
“Which browser Z supports implementation X?”, and not “Which browser Z
supports feature Y?”
> Based on your previous answer there is nothing I can find and use in my
> case (below details).
Do not jump to conclusions. Your case was not at all clear.
> Let me rephrase it. When I run the web application in the browser
> environment then I'd like to check how many times every
> functions/methods are called. The simplest way is to add console.count
> (https://developer.mozilla.org/en-US/docs/Web/API/console.count) to
> every function/method.
Thanks, I did not know of that method.
> However, I think that in reality it's not possible to do that because
> then changes needs to be done everywhere in the code. So, I thought that
> it would be better to "wrap" every function/method globally (not sure if
> I described it correctly) so that every call of function/method in web
> application first will call custom function before and put information's
> from console.count. In that way I could measure how many times every
> function/method is called. That's my goal.
It is possible to wrap all *your* functions (that you *know*) simply by
replacing them (I do something similar in jsx.dom.addEventListener() in
order to emulate EventTarget::addEventListener() when unavailable):
var myFunction = function () {
console.log("myFunction");
};
/**
* Adds a call counter to a function.
*
* NOTE: Must pass object reference and property name for a general
* solution because there is no call-by-reference
*
* @param {Object} obj
* @param {String} propName
* @return {Function}
* Decorated function
*/
function addCounter (obj, propName)
{
var original_function = obj[propName];
if (typeof original_function != "function")
{
throw new TypeError(
"addCounter: obj[propName] must refer to a function");
}
var decorated = function () {
console.count("myFunction");
original_function.apply(this, arguments);
};
obj[propName] = decorated;
return decorated;
}
/*
* Example: in global context; cannot generally decorate local functions
*/
addCounter(this, "myFunction");
However, as I have (re)discovered today thanks to your posting, it is easier
to use the Firebug profiler. Go to the “Console” tab, select “Profile”, and
select “Profile” again when you are done. The second column of the profile
shows the number of times each function/method was called.
<https://getfirebug.com/javascript>
However, note that you will be only profiling the behavior of your code with
Mozilla JavaScript.
I could not find a similar feature in Iceweasel 30’s built-in Web Developer
Tools or in Chrome Developer Tools in Chromium 34.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-14 15:55 +0200 |
| Message-ID | <1859569.COELiHlnRc@PointedEars.de> |
| In reply to | #24832 |
Thomas 'PointedEars' Lahn wrote:
> Cezary Tomczyk wrote:
>> However, I think that in reality it's not possible to do that because
>> then changes needs to be done everywhere in the code. So, I thought that
>> it would be better to "wrap" every function/method globally (not sure if
>> I described it correctly) so that every call of function/method in web
>> application first will call custom function before and put information's
>> from console.count. In that way I could measure how many times every
>> function/method is called. That's my goal.
>
> It is possible to wrap all *your* functions (that you *know*) simply by
> replacing them (I do something similar in jsx.dom.addEventListener() in
> order to emulate EventTarget::addEventListener() when unavailable):
>
> var myFunction = function () {
> console.log("myFunction");
> };
>
> /**
> * Adds a call counter to a function.
> *
> * NOTE: Must pass object reference and property name for a general
> * solution because there is no call-by-reference
> *
> * @param {Object} obj
> * @param {String} propName
> * @return {Function}
> * Decorated function
> */
> function addCounter (obj, propName)
> {
> var original_function = obj[propName];
>
> if (typeof original_function != "function")
> {
> throw new TypeError(
> "addCounter: obj[propName] must refer to a function");
> }
>
> var decorated = function () {
> console.count("myFunction");
> original_function.apply(this, arguments);
> };
>
> obj[propName] = decorated;
>
> return decorated;
> }
>
> /*
> * Example: in global context; cannot generally decorate local functions
> */
> addCounter(this, "myFunction");
Slight improvement (untested):
/**
* Decorates a function with code to be executed before and/or after
* it is called.
*
* @param {Object|Function} obj
* @param {String|Null|Undefined} propName
* @param {Function} before
* Code to be executed before the original function. Called with
* the same <code>this</code> value and arguments as the original
* function.
* @param {Function} after (optional)
* Code to be executed after the original function. Called with
* the same <code>this</code> value as the original function.
* The arguments lists consists of the return value of the
* original function followed by its argument list.
* @return {Function} Decorated function
*/
function decorate (obj, propName, before, after)
{
var original = (propName == null ? obj : obj[propName]);
if (typeof original != "function")
{
throw new TypeError(
"decorate: obj[propName] or obj must refer to a function");
}
var decorated = function () {
if (typeof before == "function")
{
before.apply(this, arguments);
}
var result = original.apply(this, arguments);
if (typeof after == "function")
{
after.apply(this, [result].concat([].slice.call(arguments)));
}
};
if (propName != null)
{
obj[propName] = decorated;
}
return decorated;
}
/*
* Example: in global context; cannot generally decorate local functions
*/
myFunction = decorate(myFunction, null, function () {
console.count("myFunction");
});
decorate(foo, "bar",
function () {
baz ();
},
function (result) {
bla(result);
});
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2014-06-16 21:55 +0200 |
| Message-ID | <7aed7$539f4bb6$6def49ce$31576@nntpswitch.blueworldhosting.com> |
| In reply to | #24832 |
2014-06-14 01:57, Thomas 'PointedEars' Lahn wrote: > Cezary Tomczyk wrote: [...] >>> Learn to think in terms of versions of implementations, not browsers. >> >> Agreed. However before I start think in terms of versions of >> implementations I need to figure out those implementations > > So you should ask “Which implementation X supports feature Y?” and then > “Which browser Z supports implementation X?”, and not “Which browser Z > supports feature Y?” Thanks. >> Based on your previous answer there is nothing I can find and use in my >> case (below details). > > Do not jump to conclusions. Your case was not at all clear. Ok. >> Let me rephrase it. When I run the web application in the browser >> environment then I'd like to check how many times every >> functions/methods are called. The simplest way is to add console.count >> (https://developer.mozilla.org/en-US/docs/Web/API/console.count) to >> every function/method. > > Thanks, I did not know of that method. You welcome. [...] > It is possible to wrap all *your* functions (that you *know*) simply by > replacing them (I do something similar in jsx.dom.addEventListener() in > order to emulate EventTarget::addEventListener() when unavailable): [...] I was thinking about same approach. However, I preferred something more simpler so that it could be "applied once and works everywhere" without even updating it in the future whenever main code will change. > However, as I have (re)discovered today thanks to your posting, it is easier > to use the Firebug profiler. Go to the “Console” tab, select “Profile”, and > select “Profile” again when you are done. The second column of the profile > shows the number of times each function/method was called. > > <https://getfirebug.com/javascript> That I know. However, above do not solving 2 problems: 1. What, if you want to measure it in the product across thousands of testers around the world. Let's say the product is in testing phase. Possible solution would be send the results periodically to the server and having the web site where data from the server would be presented. 2. "Profile" measure performance in some very specific range: from start click to end click. I'd like to measure it all the time during working with application and see the results in real time. > However, note that you will be only profiling the behavior of your code with > Mozilla JavaScript. And that's another problem. > I could not find a similar feature in Iceweasel 30’s built-in Web Developer > Tools or in Chrome Developer Tools in Chromium 34. Google Chrome contains "Profiles". -- Cezary Tomczyk http://www.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-16 23:26 +0200 |
| Message-ID | <2333271.cc9OM5qQbL@PointedEars.de> |
| In reply to | #24890 |
Cezary Tomczyk wrote:
> 2014-06-14 01:57, Thomas 'PointedEars' Lahn wrote:
>> Cezary Tomczyk wrote:
>>> Let me rephrase it. When I run the web application in the browser
>>> environment then I'd like to check how many times every
>>> functions/methods are called. The simplest way is to add console.count
>>> (https://developer.mozilla.org/en-US/docs/Web/API/console.count) to
>>> every function/method.
>> Thanks, I did not know of that method.
>
> You welcome.
^are
> [...]
>> It is possible to wrap all *your* functions (that you *know*) simply by
>> replacing them (I do something similar in jsx.dom.addEventListener() in
>> order to emulate EventTarget::addEventListener() when unavailable):
> [...]
>
> I was thinking about same approach. However, I preferred something more
> simpler so that it could be "applied once and works everywhere" without
> even updating it in the future whenever main code will change.
Within limits, using object introspection, this approach can be adapted so
that it can be “applied once and works everywhere”. See also my addendum.
>> <https://getfirebug.com/javascript>
>
> That I know.
It would be good if you published your findings in advance.
> 1. What, if you want to measure it in the product across thousands of
> testers around the world. Let's say the product is in testing phase.
> Possible solution would be send the results periodically to the server
> and having the web site where data from the server would be presented.
I do not see why it would be necessary to compare the number of calls around
the world. However, I cannot advise on hypothetical scenarios if I do not
know all relevant facts.
> 2. "Profile" measure performance in some very specific range: from start
> click to end click. I'd like to measure it all the time during working
> with application and see the results in real time.
There are console.profile() and console.profileEnd() (described on the
Firebug website). As for “real time”, you will probably have to find
another way. I would have expected console.profileEnd() to return the
results as an object.
>> I could not find a similar feature in Iceweasel 30’s built-in Web
>> Developer Tools or in Chrome Developer Tools in Chromium 34.
>
> Google Chrome contains "Profiles".
Yes, but how can I get the *call count* that way?
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-06-18 21:04 +0200 |
| Message-ID | <1420002.KozvAbkZlK@PointedEars.de> |
| In reply to | #24817 |
Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>"use strict";
>
> The next version instruments more different functions.
>
> It does not try to instrument the console object. As this
> is used for debugging purposes within the instrumented
> functions, instrumenting it might cause infinite recursion.
But is is augmenting the object referred to by “console.log”, a callable
property of a host object. That is a Really Bad Idea. (And for what?)
> "use strict";
>
> const global = this;
>
> function main()
> { "use strict";
If the global code is strict code, the function code is automatically strict
code (strict mode cannot be undone for security reasons). So this
declaration is unnecessary.
> […]
> const enumerate = function( path, object )
> { "use strict";
> if( typeof global.Object.getOwnPropertyNames === "function" )
^^^^^^
This *closure* is unnecessary if you do not amok-code and start declaring
“Object” locally (or expect this from others; but the way you write code,
I can see how that expectation might easily come to your mind).
There is also no performance advantage in this because the scope chain needs
to be consulted either way. There is a clear performance disadvantage
because an extra property lookup for “global” needs to take place. An
explicit reference to the global object should only be used if absolutely
necessary. Also it makes sense to cache references locally, because the
scope chain is shorter then, which increases runtime efficiency.
> { const key = global.Object.getOwnPropertyNames( object );
> const keyLength = key.length;
> for( let i = 0; i < keyLength; ++i )
The “let” keyword is not specified in the current standard, ECMAScript 5.1
Edition. (Neither is the “const” keyword.) AFAIK, it is supported by
Mozilla JavaScript only; earlier versions only supported it if your declared
version 1.7 by “type” attribute of the “script” element.
Code posted here should be written according to the standard (Ed. 3 and
later), for a reasonable level of compatibility. Implementation-specific or
experimental features should be either avoided or clearly documented as
such.
> […]
> global.Object.getOwnPropertyNames.wasInstrumented20140618032449 =
> global.Object.wasInstrumented20140618032449 =
> global.Date.wasInstrumented20140618032449 =
> global.Number.wasInstrumented20140618032449 =
> global.String.wasInstrumented20140618032449 =
> global.Boolean.wasInstrumented20140618032449 =
> global.Function.wasInstrumented20140618032449 =
> global.console.log.wasInstrumented20140618032449 =
I do not think it is a good idea to refer to “console” as property of the
global object of one particular global execution context.
Instead of copy-pasting the ID, one should reuse a constant-like value like
this:
Object.getOwnPropertyNames[INSTRUMENTAL_PROPERTY] = true;
I find it curious that you appear to be using the not backwards-compatible
“const” keyword for anything but true constants.
Finally, your code style is still hard to read, to say the least.
For example, using a loop is indicated here instead of multiple nested
assignments:
for (var a = [["Object", "getOwnPropertyName"], "Object", "Date",
"Number", "String", "Boolean", "Function"],
i = a.length;
i--;)
{
var p = a[i];
if (Array.isArray(p))
{
global[p[0]][p[1]][INSTRUMENTED_PROPERTY] = true;
}
else
{
global[p][INSTRUMENTED_PROPERTY] = true;
}
}
The very least you should do is to indent nested assignments.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web