.keys() Underscore Low-Dash Sugar

JavaScript performance comparison

Test case created by tomByrer

Info

Why

Branched from Sugar.JS 1.2.5 vs Underscore cloning; I wanted to test only the fastest method & add Low-Dash.

Who

Branching

Please:

To-do:

Test if a Low-Dash custom build can be even faster.

Preparation code

<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/0.3.2/lodash.min.js">
</script>
<script>
var lowdash = _.noConflict();  //prevent _ from being highjacked by Underscore
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js">
</script>
<script src="https://github.com/andrewplummer/Sugar/raw/master/release/1.2.5/minified/sugar-1.2.5.min.js">
</script>
<script>
  ui.browserscope.colors = ['#FFC200', '#FFF700', '#FF8700'];

  function clone(obj) {
    var target = {};
    for (var i in obj) {
      if (obj.hasOwnProperty(i)) {
        target[i] = obj[i];
      }
    }
    return target;
  }

  function merge(destination, source) {
    for (var i in source) {
      if (source.hasOwnProperty(i)) {
        destination[i] = source[i];
      }
    }
    return destination;
  }
</script>
<script>
  Benchmark.prototype.setup = function() {
    o = {
      a: 1,
      b: 2,
      c: 3,
      d: 4,
      e: {
        a: 1,
        b: 2,
        c: 3,
        d: 4
      },
      f: {
        a: 1,
        b: 2,
        c: 3,
        d: 4
      },
      g: [{
        a: 1,
        b: 2,
        c: 3,
        d: 4
      }, {
        a: 1,
        b: 2,
        c: 3,
        d: 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
Low-Dash
var foo = lowdash.keys(o);
pending…
Underscore
var foo = _.keys(o);
pending…
Sugar
var foo = Object.keys(o);
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

2 comments

tomByrer commented :

Oops, accidentally left the unused clone() & merge() code; should be removed next version.

John-David commented :

Lo-Dash and Underscore both use native Object.keys if available. The difference is Underscore will use it even if it's shimmed or customized. So if you were to include sugar.js before Underscore, then Underscore would use the custom Object.keys that SugarJS provides. Lo-Dash will not use custom shims and instead will use its own internal fallbacks to ensure consistency.

Lo-Dash also has an additional check for if functions are passed to _.keys so it will skip the prototype property and ensure cross-browser consistency (Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1 incorrectly have enumerable Function prototypes).

Add a comment