jQuery.unique VS _.uniq Performance

JavaScript performance comparison

Test case created by EtnasSoft and last updated

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var $ = window.$;
    var _ = window._;
    var myArr = [1, 2, 1, 3, 1, 4];
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery
var result = $.unique( myArr );
pending…
Underscore
var result = _.uniq( myArr );
pending…

Compare results of other browsers

Revisions

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:

1 comment

gnz / vnk commented :

Kind of like comparing apples to bacon, isn't it? The jQuery documentation states:

jQuery.unique( array ) Returns: Array

Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.

(emphasis mine)

And then if one dived into the code, one could see that unique, like some of the stuff in jQuery, is simply wrongly named and does actually something very different. jQuery.unique is actually all about sorting. About sorting in document order. And then also, as an additional service, removing duplicates.

Add a comment