deepCopyWithoutReferences

JavaScript performance comparison

Test case created by Gille Quentin

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  function deepCopyWithoutReferences(value) {
    if (typeof(value) !== "object" || value === null) {
      return value;
    }
  
    if (Array.isArray(value)) {
      return deepArrayCopyWithoutReferences(value);
    }
  
    return deepObjectCopyWithoutReferences(value);
  };
  
  function deepArrayCopyWithoutReferences(collection) {
    return collection.map(function (value) {
      return deepCopyWithoutReferences(value);
    });
  };
  
  function deepObjectCopyWithoutReferences(source) {
    var result = {};
  
    for (var _i = 0, _Object$keys = Object.keys(source); _i < _Object$keys.length; _i++) {
      var key = _Object$keys[_i];
      var value = source[key];
      result[key] = deepCopyWithoutReferences(value);
    }
  
    return result;
  };
  
  var nestedObject = [{
      "dimensions": [{
        "runtime": {
          "common": {
            "client": null,
            "server": null
          }
        }
      }, {
        "device": {
          "android": null,
          "blackberry": null,
          "iemobile": null,
          "iphone": null,
          "ipad": null,
          "kindle": null,
          "opera-mini": null,
          "palm": null
        }
      }, {
        "environment": {
          "development": {
            "dev": null,
            "test": null
          },
          "production": {
            "stage": null,
            "prod": null
          }
        }
      }, {
        "lang": {
          "ar": {
            "ar-JO": null,
            "ar-MA": null,
            "ar-SA": null,
            "ar-EG": null
          },
          "bn": {
            "bn-IN": null
          },
          "ca": {
            "ca-ES": null
          },
          "cs": {
            "cs-CZ": null
          },
          "da": {
            "da-DK": null
          },
          "de": {
            "de-AT": null,
            "de-DE": null
          },
          "el": {
            "el-GR": null
          },
          "en": {
            "en-AU": null,
            "en-BG": null,
            "en-CA": null,
            "en-GB": null,
            "en-GY": null,
            "en-HK": null,
            "en-IE": null,
            "en-IN": null,
            "en-MY": null,
            "en-NZ": null,
            "en-PH": null,
            "en-SG": null,
            "en-US": null,
            "en-ZA": null
          },
          "es": {
            "es-AR": null,
            "es-BO": null,
            "es-CL": null,
            "es-CO": null,
            "es-EC": null,
            "es-ES": null,
            "es-MX": null,
            "es-PE": null,
            "es-PY": null,
            "es-US": null,
            "es-UY": null,
            "es-VE": null
          },
          "fi": {
            "fi-FI": null
          },
          "fr": {
            "fr-BE": null,
            "fr-CA": null,
            "fr-FR": null,
            "fr-GF": null
          },
          "hi": {
            "hi-IN": null
          },
          "hu": {
            "hu-HU": null
          },
          "id": {
            "id-ID": null
          },
          "it": {
            "it-IT": null
          },
          "ja": {
            "ja-JP": null
          },
          "kn": {
            "kn-IN": null
          },
          "ko": {
            "ko-KR": null
          },
          "ml": {
            "ml-IN": null
          },
          "mr": {
            "mr-IN": null
          },
          "ms": {
            "ms-MY": null
          },
          "nb": {
            "nb-NO": null
          },
          "nl": {
            "nl-BE": null,
            "nl-NL": null,
            "nl-SR": null
          },
          "pl": {
            "pl-PL": null
          },
          "pt": {
            "pt-BR": null
          },
          "ro": {
            "ro-RO": null
          },
          "ru": {
            "ru-RU": null
          },
          "sv": {
            "sv-SE": null
          },
          "ta": {
            "ta-IN": null
          },
          "te": {
            "te-IN": null
          },
          "th": {
            "th-TH": null
          },
          "tr": {
            "tr-TR": null
          },
          "vi": {
            "vi-VN": null
          },
          "zh": {
            "zh-Hans": {
              "zh-Hans-CN": null
            },
            "zh-Hant": {
              "zh-Hant-HK": null,
              "zh-Hant-TW": null
            }
          }
        }
      }]}];
  

};
</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
Lodash clopeDeep
var dimensionsCopy = _.cloneDeep(nestedObject);
pending…
Json parse / stringify
var dimensionsCopy = JSON.parse(JSON.stringify(nestedObject));
pending…
deepCopy
var dimensionsCopy = deepCopyWithoutReferences(nestedObject);
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