Underscore Sort/Unique order

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
Benchmark.prototype.setup = function() {
    var values = []
    for (var i = 0; i < 10000; i++) {

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
uniq, sort
var x = _.uniq(values);
_.sortBy(x, _.identity);
sort, uniq
var x = _.sortBy(values, _.identity);
_.uniq(x, true);

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


David Schalk commented :

I messed around with the underscore.js code for _.uniq this afternoon because I couldn't get it to sort the unique array elements. A code example I found online which apparently worked for somebody didn't work for me. I don't see how the underscore.js code for _.uniq could sort anything but the function still asks for "isSorted."

How fast is this method for eliminating duplicate values?:

 Array.prototype.unique = function () {
    var o = {}, i = 0, l = this.length, r = [];
for (i=0; i<l;i+=1) o[this[i]] = this[i];
for (var n in o)  {r.push(o[n]);}
return r;

I can't take credit for thinking of it. I just think using the uniqueness of index values to obtain unique elements is surprisingly elegant. I'm rooting for it in the Browserscope test, in case you go ahead and compare it to _.unique().

John-David Dalton commented :

Your Array#unique implementation will conflate values like the string "2" and the number 2.

David Schalk commented :

Thanks. I have been using this method on two dimensional arrays of randomly generated integers to avoid unnecessary computations when repeat values happen to show up. It serves me well for that limited purpose but realize now that it could not be relied upon generally.

By the way, the arrays of integers are for a work in progress at http://score.nodejitsu.com/, an online version of a game my twelve-year-old son plays with four dice: two six-sided, one twelve-sided, and one with twenty sides. The first person to see how addition, subtraction, multiplication, division, and/or concatenation can get the number 20 using three or four of the dice clicks "SCORE" and has limited time in which to show the calculation. Clicking "IMPOSSIBLE" starts a one-minute countdown for the other players to find a solution. The computer comes up with solutions most people wouldn't see, such as concatenating 5 behind 6 and 11 behind 20 and then dividing 56 into 1,120 to get 20. It's much easier to see (6 - 5) * 20 = 20.

Again, thanks for the insight.

David Schalk commented :

Duh. underscore isn't supposed to sort. I thought I saw an example of it sorting on a blog and I didn't read the underscore documentation carefully enough. Maybe it was an earlier version of underscore; or maybe a late-night mirage?

You have been a big help to me. I didn't know about lodash until I saw your tweets.

Comment form temporarily disabled.

Add a comment