Javascript Object deep copy one-liner My goal was to create a single-line function that produces a deep copy of a javascript object.

Requirements:


Executable on a single line without the use of semicolons (exception: last character of the line)
Nested objects should not be references to the originals


Function:

let copiedObjct = (copy = (obj) =&gt; (Object.keys(obj).reduce((v, d) =&gt; Object.assign(v, {[d]: (obj[d].constructor === Object) ? copy(obj[d]) : obj[d]}), {})))(obj);

Usage (expanded):

let obj = { /*...*/ }
let copiedObjct = (copy = (obj) =&gt; (
  Object.keys(obj).reduce((v, d) =&gt; Object.assign(v, {
    [d]: (obj[d].constructor === Object) ? copy(obj[d]) : obj[d]
  }), {})
))(obj);


My questions are:


Is this code efficient?
Are there any reasons this shouldn't be used in the wild?
Is there a way I can make it shorter?
Is this code efficient?


Probably... not.

Efficiency can mean different things. Efficiency could mean "fast running code" or "use the least amount of memory" - this can only be determined by running the code through a profiler. Efficiency could also mean "I can use it easily" or "works reliably" or "handles edge cases well" - this can only be determined by having other people use it.


  Are there any reasons this shouldn't be used in the wild?


Yes.

Does the code really run on a variety of objects? What if I put things like Arrays, Dates, RegExps, Maps, Sets, etc. in my object? Does it handle circular references? This is a naive implementation of deep-copy which is fine in small apps and prototypes where you know what the data looks like. But in more general cases, this will probably not hold.

If you'd like to see how a general-purpose deep-copy function looks like, see how jQuery.extend or lodash.assign is implemented.


  Is there a way I can make it shorter?


Wrong question. Code size is irrelevant in JS, you have minifiers for that.