Reol.js vs naive search

JavaScript performance comparison

Revision 10 of this test case created by Andreas Hultgren

Info

Testing how much faster Reol.js (indexed array-thingy) is than a plain ol' loop-through-the-array-til-a-match-is-found.

See https://github.com/ahultgren/Reol.js

edit: added the .length to the bSearch/sSearch

Preparation code

<script>
require=function(t,n,i){function r(i,o){if(!n[i]){if(!t[i]){var s="function"==typeof require&&require;if(!o&&s)return s(i,!0);if(e)return e(i,!0);throw Error("Cannot find module '"+i+"'")}var u=n[i]={exports:{}};t[i][0].call(u.exports,function(n){var e=t[i][1][n];return r(e?e:n)},u,u.exports)}return n[i].exports}for(var e="function"==typeof require&&require,o=0;i.length>o;o++)r(i[o]);return r}({Reol:[function(t,n){n.exports=t("oF0Uok")},{}],oF0Uok:[function(t,n){(function(){"use strict";n.exports=t("./lib/Reol"),"undefined"!=typeof window&&(window.Reol=n.exports)})()},{"./lib/Reol":1}],1:[function(t,n){"use strict";function i(t){var n;r.call(this),this.index={},this.indexes={};for(n in t)this.index[n]=new e(n,t[n]),this.indexes[n]=t[n];return this}var r=t("./List"),e=t("./Index");i.List=r,i.prototype=new r,i.prototype.constructor=i,i.prototype.add=function(t,n){var i,r;if("object"!=typeof t){if(i=Error("Sorry, this class only works with objects."),!n)throw i;n(i)}this.push(t);for(r in this.indexes)this.index[r].add(t);return n&&n(),this},i.prototype.find=function(t,n,i){var r,e,o;n=n||function(){};for(e in t)if(t[e]){r=e;break}return r?(o=this.index[r]?this.findInIndex(r,t[r]):this.findInList(r,t[r],i),o=!o&&[]||void 0!==o.length&&o||[o],n(null,o),o):(n(this.toArray()),this.toArray())},i.prototype.findOne=function(t,n){return this.find(t,function(t,i){n&&n(t,i[0])},!0)[0]},i.prototype.findInIndex=function(t,n){return this.index[t].find(n)},n.exports=i},{"./List":2,"./Index":3}],2:[function(t,n,i){"use strict";function r(t){return function(n){var i;for(i in t)if(e.findByPath(n,i)!==t[i])return!1;return!0}}var e,o=t("./utils");i=n.exports=e=function(t){o.extend(this,t)},e.findByPath=function(t,n){var i,r;for(n=n.split("."),r=t;n.length&&r;)i=n.shift(),r=r[i];return r},e.prototype=[],e.prototype.add=function(t){this.push(t)},e.prototype.merge=function(t,n){var i,r;for(i=0,r=t.length;r>i;i++)this.add(t[i]);return n&&n(),this},e.prototype.findInList=function(t,n,i){var r,e,o=[],s=this;for(r=0,e=s.length;e>r;r++)if(s[r][t]===n){if(i)return s[r];o.push(s[r])}return o},e.prototype.toArray=function(){return[].slice.call(this)},e.prototype.filter=function(t){var n,i,e=new this.constructor,o="function"==typeof t?t:r(t);if(Array.prototype.filter)return e.merge([].filter.call(this,o));for(n=0,i=this.length;i>n;n++)r(this[n])&&e.add(this[n]);return e}},{"./utils":4}],3:[function(t,n,i){"use strict";var r,e=t("./List"),o=t("./Bucket"),s=t("./utils").extend;r=i=n.exports=function(t,n){return this._index=t,this._settings=s({unique:!1,sparse:!1},"object"==typeof n&&n||{}),this},r.prototype=new e,r.prototype.constructor=r,r.prototype.add=function(t){var n,i,r=this._index;return r.indexOf(".")?n=e.findByPath(t,r):void 0!==t[r]&&(n=t[r]),void 0===n&&this._settings.sparse===!0?!1:(i=this[n],i||(this[n]=i=new o(this._settings.unique)),i.add(t),!0)},r.prototype.find=function(t){return this[t]}},{"./List":2,"./Bucket":5,"./utils":4}],4:[function(t,n,i){"use strict";i.extend=function(t){for(var n,i,r=[].slice.call(arguments);n=r.shift();)for(i in n)t[i]=n[i];return t}},{}],5:[function(t,n,i){"use strict";var r,e=t("./List");r=i=n.exports=function(t){this.unique=t},r.prototype=new e,r.prototype.constructor=r,r.prototype.add=function(t){this.length&&this.unique||this.push(t)}},{"./List":2}]},{},["oF0Uok"]);
</script>
<script>
Benchmark.prototype.setup = function() {
    var list, array;
   
    function makeList (list, array, max) {
      for(var i = max; i--;) {
        list.add({
          test1: 'meow' + i,
          test2: 'mooo' + i,
          test3: 'mooo' + i,
          test4: 'mooo' + i,
          test5: 'mooo' + i,
          test6: 'mooo' + i,
          test7: 'mooo' + i,
          test8: 'mooo' + i,
          test9: 'mooo' + i
        });
   
        array.push({
          test1: 'meow' + i,
          test2: 'mooo' + i,
          test3: 'mooo' + i,
          test4: 'mooo' + i,
          test5: 'mooo' + i,
          test6: 'mooo' + i,
          test7: 'mooo' + i,
          test8: 'mooo' + i,
          test9: 'mooo' + i
        });
      }
    }
   
    // Big
    list = new Reol({
      test1: true,
      test2: true
    });
    array = [];
   
    makeList(list, array, 1000);
   
   
    function find(array, key, value) {
      for(var i = 0, l = array.length; i < l; i++) {
        if(array[i][key] === value) {
          return array[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
Naive find
find(array, 'test1', 'meow1');
find(array, 'test2', 'mooo500');
find(array, 'test1', 'meow999');
pending…
.find()
list.find({ test1:  'meow1' });
list.find({ test2:  'mooo500' });
list.find({ test1:  'meow999' });
pending…
.findInIndex()
list.findInIndex('test1', 'meow1');
list.findInIndex('test2', 'mooo500');
list.findInIndex('test1', 'meow999');
pending…
.findInList()
list.findInList('test1', 'meow1', true);
list.findInList('test2', 'mooo500', true);
list.findInList('test1', 'meow999', true);
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:

0 comments

Add a comment