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)
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.
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
Hey John, I appreciate you for your help!!
I Think You are very clever. Thankyou
Very helpful, thanks!
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.
I know there is not compare function, but was not this good enough?
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)
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.
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
Why not use the native filter for primitives?
I understand this won’t work for complex objects and if you want them sorted, you’ll have to do that afterwards.
It can actually be just a little faster than that using the third parameter::
It can actually be just a little faster than that using the third parameter:
(Corrected the variable names)
Is it “just a little faster” because the variable,
ary, is lower in the scope chain thanarray? Or is there some special magic here?