insert vs underscore binary

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
   
    var _views = [];
    var _currentIndex = -1;
    var a = [];
    var aIndex = -1;
   
    var _insert =  function(uid) {
                var found = false;
                for (var i = 0; i < _views.length; i++) {
                    var currentUid = _views[i];
                    if (uid < currentUid) {
                        _views.splice(i, 0, uid);
                        _currentIndex = i;
                        found = true;
                        break;
                    } else if (uid === currentUid) {
                        _currentIndex = i;
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    _views.push(uid);
                    _currentIndex = i;
                }
            };
   
   
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
insert
for (var i = 10; i >= 0; i++) {
   _insert(i);
}
pending…
underscore sorted insert
for (var i = 10; i >= 0; i++) {
   aIndex = _.sortedIndex(a, i);
   a.splice(aIndex, 0, i);
}
 
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment