Clone function or underscore

Benchmark created by Rankill on


Preparation HTML

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

Setup

var results;
    var objects = {
      id: 3,
      user_id: 3,
      gender: 'male',
      birthdate: '1978-4-26',
      city: 'Copenhagen',
      country_id: 58,
      active: 1,
      visible: 1,
      timestamp: 1348659760,
      photo_id: 1386,
      description_id: 20
    }
    
      function clone(obj) {
        if (null == obj || "object" != typeof obj) return obj;
        var copy = obj.constructor();
        for (var attr in obj) {
          if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
        }
        return copy;
      }

Test runner

Ready to run.

Testing in
TestOps/sec
With function
var res;
res = clone(objects)
ready
With underscore
var res;
res = _.clone(objects)
ready

Revisions

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