www.webdeveloper.com
Results 1 to 9 of 9

Thread: Shorten this code?

  1. #1
    Join Date
    May 2012
    Posts
    16

    Shorten this code?

    Is there a way to short and make this function faster, smaller and better?

    function rand() {
    var r = Math.floor(Math.random()*array.length);
    document.getElementById('obj').innerHTML = array[r];
    }

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Exclamation

    Quote Originally Posted by WayneISWayne View Post
    Is there a way to short and make this function faster, smaller and better?

    function rand() {
    var r = Math.floor(Math.random()*array.length);
    document.getElementById('obj').innerHTML = array[r];
    }
    Don't know why you would want to do that but...
    Code:
    function rand() {
      document.getElementById('obj').innerHTML = array[Math.floor(Math.random()*array.length)];
    }
    Unless you are repeating it like a million times, I don't think the space or time difference will be significant!

  3. #3
    Join Date
    May 2012
    Posts
    16
    Thank you so much! I'm trying to make a really small JavaScript for less load time etc... and better on benchmarks too.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    A shorter method ?
    Code:
    function rand() {
      document.getElementById('obj').innerHTML = array[~~(Math.random()*array.length)];
    }

  5. #5
    Join Date
    May 2012
    Posts
    16
    Quote Originally Posted by 007Julien View Post
    A shorter method ?
    Code:
    function rand() {
      document.getElementById('obj').innerHTML = array[~~(Math.random()*array.length)];
    }
    Dose the tilde work on all browsers?

  6. #6
    Join Date
    Aug 2007
    Posts
    3,767
    Well, if you are repeating that a lot, it's not good to be calling document.getElementById so much. Storing a reference would probably be better. Short code doesn't necessarily mean it's faster.
    Great wit and madness are near allied, and fine a line their bounds divide.

  7. #7
    Join Date
    May 2012
    Posts
    16
    Quote Originally Posted by Declan1991 View Post
    Well, if you are repeating that a lot, it's not good to be calling document.getElementById so much. Storing a reference would probably be better. Short code doesn't necessarily mean it's faster.
    I know. If I do that, there would be a lot of function calls. I wish there was a better way of doing the getElementById.

  8. #8
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    As an example, you can see this page which play chess (no castling and en passant capture) with a 1023 bytes javascript !

  9. #9
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by WayneISWayne View Post
    I know. If I do that, there would be a lot of function calls. I wish there was a better way of doing the getElementById.
    Code:
    var rand = ( function () 
    {   
      var elem = document.getElementById( 'obj' );
    
      return function()
      {
        elem.innerHTML = array[ Math.floor( Math.random() * array.length ) ]; 
      }  
    
    } )();
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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