Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    filter() works in almost all browsers in common use and uses compiled processing built into the browser making it a lot faster as well as a lot shorter.

    That you have to add a half dozen lines of extra code for the few browsers that don't yet support it that will add one extra comparison to what needs to run in the browsers that do makes your code easier to maintain than using a different way of coding. Once all browsers in use do support it you simply delete those extra lines of code.

    Using an alternative approach just because one commonly used browser doesn't support it means that once that browser dies out you still have your far longer harder to maintain code waiting to be rewritten into something that is both a lot shorter and a lot faster.

    If you want shorter easier to maintain code you'd use filter. That is is faster as well is a bonus.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  2. Users who have thanked felgall for this post:

    rnd me (12-03-2012)

  3. #17
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by felgall View Post
    If you want shorter, easier to maintain code you'd use filter. That it is faster as well is a bonus.
    welcome to the future, may it serve you well!
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  4. #18
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Note that filter is also much shorter to code and test in the first place as you have only one line of code that you have written that needs testing as compared to the alternative where you have to write a lot more code including a loop.

    You can easily implement filter for any of the browsers that do not yet support it by grabbing a copy of the code to add that support from the Mozilla JavaScript reference. You would not need to worry about testing that code any more than you'd need to worry about testing all the code in JQuery (if you use that JavaScript library) as the code has already been tested.


    Why code and test an entire loop of code that needs to do the same thing as can already be easily done with only one line of new code.
    Last edited by felgall; 12-03-2012 at 08:17 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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