Sorry, no posts matched your criteria.
  1. armdros
    | #1

    Hey John, I appreciate you for your help!!
    I Think You are very clever. Thankyou

  2. | #2

    Very helpful, thanks!

  3. | #3

    Another great post as usual. Had checked out prototype.js sometime back but never dojo. Now that you mention it, will surely try my hands on it.

  4. | #4

    I know there is not compare function, but was not this good enough?

    for(var i = 1, length = array.length, n; i < length; ++i){
        if(-1 < (n = array.indexOf(array[i - 1], i))){
            a.splice(n, 1);
            --length;
            --i;
        }
    };

    an optimized fast performances version could avoid the inline splice and store those index to discard (splice could cost a lot) in order to push only certain indexes (not duplicated)

  5. | #5

    Interesting! I wouldn’t have thought to use indexOf() or splice() since I just assumed those were expensive operations. I see you’ve minimized the impact of scanning the array by using the second argument for indexOf(). Clever. Some day I’ll do some real benchmarking to see what works best.

  6. | #6

    indexOf is unfortunately expensive only in IE where it’s not native while splice could be expensive but above for loop is something extremely easy to maintain (5 line of code that should just work).
    On the other hand I am using on up to two native methods against sort + forEach + map + push + all callbacks involved and executed for each index so I am kinda sure mine will perform 2 up to 5 times faster (in IE as example just sort is expensive).
    Finally, if the goal is to make something portable for every case, objects included, with mine you are sure about unique primitives/instances so it depends what you need (if you need just to compare a single object property, mine won’t make sense)
    Regards

  7. | #7

    Why not use the native filter for primitives?

    array.filter(function(item, i) {
      return array.indexOf(item) == i;
    });

    I understand this won’t work for complex objects and if you want them sorted, you’ll have to do that afterwards.

  8. | #8

    It can actually be just a little faster than that using the third parameter::

    array.filter(function(value, i, ary) {
      return ary.indexOf(item) == i;
    });
  9. | #9

    It can actually be just a little faster than that using the third parameter:

    array.filter(function(item, i, ary) {
      return ary.indexOf(item) == i;
    });

    (Corrected the variable names)

  10. | #10

    Is it “just a little faster” because the variable, ary, is lower in the scope chain than array? Or is there some special magic here?

Comment pages
  1. | #1
  2. | #2
  3. | #3
  4. | #4
  5. | #5
  6. | #6
  7. | #7
  8. | #8
  9. | #9
  10. | #10
  11. | #11
  12. | #12
  13. | #13
  14. | #14
  15. | #15
  16. | #16
  17. | #17
  18. | #18
  19. | #19
  20. | #20
  21. | #21
  22. | #22
  23. | #23
  24. | #24
  25. | #25
Comments are closed.