I am assuming this is for AS2 - right?
If you want speed that probably means you are dealing with a lot of data(?)
1- What is the typical "recursion" level?
2- What is the typical number of items?
3- What is the typical size of "sub arrays"?
In general making a function call is not fast at all. Better iterate than
recurse.
B.
2006/7/25, Steven Sacks | BLITZ <[EMAIL PROTECTED]>:
Is there a way to make this script any faster?
Array.prototype.flatten = function(r) {
if (!r) r = [];
var l = this.length;
for (var a = 0; a < l; a++) {
if (this[a].__proto__ != Array.prototype) {
r.push(this[a]);
} else {
this[a].flatten(r);
}
}
return r;
}
This function takes an array and flattens it, meaning any nested arrays
will get flattened into a single array.
Example:
x = ["a", "b", "c", ["d", "e"], "f", ["g", ["h"]], [[], "i"], "j"];
y = x.flatten();
y >> ["a","b","c","d","e","f","g","h","i","j"]
Issues:
Array.reverse() and Array.unshift() are notoriously slow and any speed
gained from doing a reverse while loop would be lost.
I don't see how this script could be sped up, but I'm not a recursion
expert. The current speed increases I have are:
1) Single character variable names
2) this.length stored in a variable avoids computation every loop.
3) Most common if true (not a nested array) comes first
I'm not clear which, if either, is faster:
if (x != y) vs if (!(x == y))
That's the only other place I can see a spot for a possible speed
improvement.
Thanks!
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com