View Full Version : method or script tags
epopov
02-19-2004, 03:04 AM
Hi,
I have many global functions, i.e. all parts of my app need to use them. What is better: to represent them as methods of canvas or as functions in tags script? What case is more optimal for performance/app size?
tspratt
02-19-2004, 09:44 AM
I mostly prefer to use scripts, mainly because they are more maintainable: A single location within a script and CDATA tag, declaration and arguments are clearer and more flexible, and comments are clearer.
One note, If you are using an LzDelegate, it requires a context (canvas), therefore you need to use a global method.
Tracy
antun
02-19-2004, 10:08 AM
I did a little experiment, and found that if you used <method> tags, instead of functions in a <script> tag the total file size was a little smaller:
Canvas (with debugger) 154K (157,414 bytes)
10 Methods in canvas 154K (157,583 bytes)
10 Functions in script 154K (157,650 bytes)
As for which is more efficient, I think functions are, but only by a little. This is probably because functions aren't attached to a view in the viewsystem hierarchy. Here's how I tested it:
<canvas debug="true">
<method name="method1">
<![CDATA[
var d = new Date()
var startTime = d.getTime();
var a = 0;
for ( var i=0; i<1000000; i++ ) {
a = !a;
}
var d = new Date()
var endTime = d.getTime();
var timeTaken = endTime - startTime;
Debug.write( "Took: " + timeTaken );
]]>
</method>
<script>
<![CDATA[
function method2() {
var d = new Date()
var startTime = d.getTime();
var a = 0;
for ( var i=0; i<1000000; i++ ) {
a = !a;
}
var d = new Date()
var endTime = d.getTime();
var timeTaken = endTime - startTime;
Debug.write( "Took: " + timeTaken );
}
]]>
</script>
<simplelayout axis="y" spacing="10" />
<button>Use method
<method event="onclick">
canvas.method1();
</method>
</button>
<button>Use function
<method event="onclick">
method2();
</method>
</button>
</canvas>
I averaged out 10 goes on each, and got these results:
Method: 8540 ms
Function: 7785 ms
Remember this is over a million iterations, so the difference isn't that big. If they are utility functions, you could go either way. If you have some utility functions from a library that you've used in other JavaScript applications, and they work fine in a <script> tag, then there's no reason not to use them there.
If they aren't really utility functions, for example they act upon your application in some way, then it might be worthwhile to write methods.
-Antun
I create a seperate file called globalcode.lzx.
Then I use:
<node id="g">
<attribute name="globalVar"/>
<method name="startup"/>
</node>
When my app gets large it is a relief to be able to go to one file and find what I'm looking for.
Calls are then nice and simple:
g.startup();
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.