Underscore extend() vs defaults()

Benchmark created on


Description

Underscore's extend() and defaults() functions can be used to achieve a similar result, by reversing the arguments. The object that is modified will be the first argument passed to either function, but the final value of the first argument will be the same in either case.

Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>

Setup

var foo = {
      a: 1,
      b: 2,
      c: 3,
      d: 4,
      e: 5
    };
    
    var bar = {
      b: 1,
      c: 2,
      d: 3,
      e: 4,
      f: 5
    };
    
    var baz = {
      c: 1,
      d: 2,
      e: 3,
      f: 4,
      g: 5
    };

Test runner

Ready to run.

Testing in
TestOps/sec
extend()
_.extend(foo, bar, baz);
ready
defaults()
_.defaults(baz, bar, foo);
ready
extend() 2
_(foo).extend(bar, baz);
ready
defaults() 2
_(baz).defaults(bar, foo);
ready

Revisions

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