www.webdeveloper.com
Page 4 of 15 FirstFirst ... 2345614 ... LastLast
Results 46 to 60 of 221

Thread: Here are 2 prototype-functions I created.

  1. #46
    Join Date
    Apr 2005
    Posts
    92
    Timing for optimization purposes:

    Code:
    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);

  2. #47
    Join Date
    Apr 2005
    Posts
    92
    To remove an element from an array and shrink it
    Code:
    Array.prototype.removeElement = 
      function(obj) {
        for (var i=0;i<this.length;i++) {
          if (this[i] == obj) {
            this[i] = this[this.length-1];
            this.pop();
            break;
          }
        }
      };
    Only use this if the order of the elements in the array is not important

  3. #48
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Quote Originally Posted by tilspam@ofir.dk
    To remove an element from an array and shrink it
    Code:
    Array.prototype.removeElement = 
      function(obj) {
        for (var i=0;i<this.length;i++) {
          if (this[i] == obj) {
            this[i] = this[this.length-1];
            this.pop();
            break;
          }
        }
      };
    Only use this if the order of the elements in the array is not important
    If the order is important then what's wrong with this small set of changes?
    PHP Code:
    Array.prototype.removeElement = function(){
      var 
    found false;
      for (var 
    i=0i<this.lengthi++) {
        if (
    this[i] == arguments[0]) {
          
    found true;
        }
        if(
    found && this.length-!= i){
         
    this[i] = this[i+1];
        };
      };
      
    this.pop();
    }; 
    Last edited by scragar; 05-10-2005 at 08:38 AM.

  4. #49
    Join Date
    Apr 2005
    Posts
    92
    Quote Originally Posted by scragar
    If the order is important then what's wrong with this small set of changes
    Probably nothing ... but this is faster

    Code:
    Array.prototype.removeElement =
      function() { 
        for (var i=0; i<this.length; i++) { 
          if (this[i] == arguments[0]) {
            for (var j=i;j<this.length-1;j++)
              this[j] = this[j+1];
            this.pop();
            break;
          }
        }
      };
    Last edited by tilspam@ofir.dk; 05-10-2005 at 09:34 AM.

  5. #50
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    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;
    };
    Last edited by BigMoosie; 05-10-2005 at 10:10 PM.

  6. #51
    Join Date
    Apr 2005
    Posts
    92
    BigMoosie> Instead of pop'ing count times use slice. Replace your 2nd for loop with

    Code:
    this = this.slice(0,count);

  7. #52
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by tilspam@ofir.dk
    Timing for optimization purposes:

    Code:
    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).

  8. #53
    Join Date
    Apr 2005
    Posts
    92
    or even better use Array.splice()

    Code:
    this.splice(this.length-count);

  9. #54
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    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.

  10. #55
    Join Date
    Apr 2005
    Posts
    92
    Quote Originally Posted by BigMoosie
    ...but do note that splice requires a second argument (where to stop cutting)...
    No. Second argument is optional. If not supplied splice will delete all elements from the value of the first argument untill the end of the array.

    Code:
    var arr1 = new Array(1,2,3,4,5,6,7);
    var arr2 = new Array();
    arr2 = arr1.splice(4);
    
    // arr1 is now [1,2,3,4] and arr2 is [5,6,7]

  11. #56
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Not in Internet Explorer it isnt.

  12. #57
    Join Date
    Apr 2005
    Posts
    92
    Your right...but according to JS The Definitive Guide the behaviour should be as in my last post. Guess its a bug in IE then...

  13. #58
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887

    pack()

    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 02:32 PM. Reason: fixed an error

  14. #59
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    I use this:

    Code:
    <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>
    Eric
    Last edited by A1ien51; 12-01-2005 at 10:02 PM.

  15. #60
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Woah! That one is very useful! Thanks Eric!
    That is a great way to find the number of days in each month.

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles