A few thoughts on optimization. Just thoughts.
Steven Sacks | BLITZ wrote:
// Remove all elements from Array
Array.prototype.clear = function() {
this.length = 0;
};
This function scares me to death. I understand that the garbage
collection in any decent VM should take care of this... but wow, it
feels like some dangerous voodoo. :)
// Removes all null and undefined elements from Array
Array.prototype.compact = function() {
this.delete_all(null);
this.delete_all(undefined);
};
Which is faster, iterating over an array of size N once and checking
for elements that are either null or undefined in a single pass, or
iterating over an array of size N checking for nulls and then
iterating again over an array of size N-x and checking for the
undefineds?
// Deletes items from the Array that are equal to obj
Array.prototype.delete_all = function(obj) {
var i = -1;
var a = this.length;
while (--a -(-1)) {
if (this[a] == obj) {
i = a;
break;
} else if (this[a] instanceof Array && obj instanceof
Array) {
if (this[a].eql(obj)) i = a;
break;
}
}
if (i > -1) {
this.splice(i, 1);
this.delete_all(obj);
}
};
You do this sort of thing in several of your functions and I think you
_might_ be able to squeeze a bit of theoretical speed out of things by
if obj is an array
loop checking for array equality
else
loop checking for value equality
In stead of
loop over the entire array
check value equality
if obj is an array
check array equality
It's not much, but over a few thousand comparrisions it might make a difference?
Also, what's the performance hit for splice()? I know it's bad in some
languages. Would it be potentially faster to build a new array in
stead of re-splicing the old one? I guess it depends on the length of
the array as compared to the number of elements being removed. What is
the threshold where one becomes preferrable to the other?
// Runs a function on every item in the array
// Returns an array of all items for which the function returns true.
Array.prototype.filter = function(block) {
var r = [];
var n = this.length;
for (var a = 0; a < n; a++) {
if (block(this[a])) r.push(this[a]);
}
return r;
};
I love this one. I use it absolutely all the time in other languages.
Ammon
_______________________________________________
[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