jQuery.unique VS _.uniq Performance

JavaScript performance comparison

Test case created by EtnasSoft and last updated

Preparation code

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

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
var result = $.unique( myArr );
var result = _.uniq( myArr );

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:

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.

Comment form temporarily disabled.

Add a comment