Function.prototype.time =
function() {
var sTime = new Date();
this.apply(null,arguments);
var eTime = new Date();
return eTime.getTime() - sTime.getTime();
};
Say you have a function f. To time the function with arguments arg0, arg1 & arg2 do
This is not as fast but more useful, it can take 2 arguments, the first is the same as yours (the value to be removed) however the second defines how many of it to remove, if the second is not sent then it will simply remove them all:
Code:
Array.prototype.removeElement=function() {
var arrayLength=this.length;
var count=0;
var limit=arguments[1];
if (limit==null) limit=arrayLength;
for (var i=0; i<arrayLength; i++){
if (this[i]==arguments[0] && count<limit) count++;
else this[i-count]=this[i];
}
this.splice(this.length-count, this.length);
return count;
};
Function.prototype.time =
function() {
var sTime = new Date();
this.apply(null,arguments);
var eTime = new Date();
return eTime.getTime() - sTime.getTime();
};
Say you have a function f. To time the function with arguments arg0, arg1 & arg2 do
Code:
f.time(arg0,arg1,arg2);
You only need to use eTime-sTime.
There are two ways of measuring the speed of these Array prototypes, the wrong way is to do it a few thousand times on a short array, make sure that you are timing it once on a huge (or as ultimater always says: hugh) array (I dont doubt that you already know this, but just for others).
Thanx for that I made the fix (the first one didnt work because you cannot directly change this), but do note that splice requires a second argument (where to stop cutting). I also made it return count.
String.prototype.pack is a function that packs the given string to the given amount of characters. If a string is 3 characters long and you pack(10) it then the string becomes 7 spaces longer. If you pack(10) a string of 50 characters, it is cut to only the first 10 characters. So either way your packing the string to 10 characters with pack(10).
HTML Code:
<script type="text/javascript">
String.prototype.multipulCopies=function(){var temp="";for(var i=0;i<arguments[0];i++){temp+=this}return temp}
String.prototype.pack=function(){var tempS=this.substr(0,arguments[0]);var tempL=arguments[0]-this.length;var spaces=" ".multipulCopies(tempL);return tempS+spaces}
alert("this message is too big!".pack(5))
alert("a".pack(10)+"b")
</script>
The pack function uses the multipulCopies function when it is defined.
Last edited by Ultimater; 05-12-2005 at 01:32 PM.
Reason: fixed an error
<script>
Date.prototype.NumberOfDays =
function(){
this.setDate(1); //set date to first avoids problem with 31/30 day months!
this.setMonth(this.getMonth()+1);
this.setDate(0);
return this.getDate();
}
var today = new Date()
alert(today.NumberOfDays())
</script>
Bookmarks