shallow copying

JavaScript performance comparison

Test case created by

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var original = {
      foo: {
          bar: [1,2,3,4,5]
      },
      bar: ['foo', 'bar', 'baz']
  };

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
json
var newObject = JSON.parse(JSON.stringify(original));
pending…
reduce
var newObject = Object.keys(original).reduce(function (obj, item) {
    obj[item] = original[item];
    return obj;
},{});
pending…
foreach
var newObject = {};
Object.keys(original).forEach(function (item) { newObject[item] = original[item]; });
pending…
underscore
var newObject = _.clone(original);
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.

0 Comments